static-initialization Questions

4

Solved

Everything I found across the internet about static initialization order fiasco was about C++, but is it true that if I initialize global variable of some type Foo like struct Foo { int flag; pth...
Burbage asked 16/11, 2023 at 13:49

4

Solved

Once I was reading an awesome C++ FAQ (It is really good!!) and read the topic about how to prevent the static initialization order "fiasco". So the author advises to wrap the static variables into...

0

Consider a global (namespace scope) variable declared using the new inline variable feature in C++ 17: struct something { something(); ~something(); }; inline something global; In Clang 14 on x...
Normandnormandy asked 13/7, 2022 at 19:56

5

Solved

Consider this example code: template<class D> char register_(){ return D::get_dummy(); // static function } template<class D> struct Foo{ static char const dummy; }; template<cl...
Globigerina asked 21/6, 2011 at 6:0

2

Solved

I manage an open source project and have a user reporting a situation which I think is impossible according to Java's order of initialization of static variables in classes. The value of a static f...
Hyperventilation asked 16/7, 2021 at 20:59

1

Solved

When compiling this code in latest verson of gcc (or clang) with -std=c17 -pedantic-errors -Wall -Wextra static const int y = 1; static int x = y; then I get no compiler diagnostic message even th...
Profiterole asked 5/7, 2021 at 8:0

2

Solved

Example: struct Foo { Foo() { printf("foo\n"); } }; static Foo foo; __attribute__((constructor)) static void _bar() { printf("bar\n"); } Is it deterministic wether foo or bar is printed first? ...
Abridge asked 8/12, 2011 at 15:37

6

Solved

The typical way of creating a Javascript object is the following: var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; I need to create such a map where both keys and values are S...
Anabolism asked 5/2, 2013 at 16:17

2

I have gone through some related topics on internet like this and questions here like this, this and this, but I'm getting nowhere. Here is my simplified code: MainActivity: package com.test.static...
Nogging asked 12/12, 2020 at 18:20

3

Solved

We know that local static variable initialization is thread-safe in C++11, and modern compilers fully support this. (Is local static variable initialization thread-safe in C++11?) What is the cost...
Anemone asked 15/7, 2016 at 8:7

4

Solved

Given a static class with an initializer method: public static class Foo { // Class members... internal static init() { // Do some initialization... } } How can I ensure the initializer is...

1

Solved

language: java version: 12.0.2 String source code as follows: /* @implNote * The actual value for this field is injected by JVM. The static * initialization block is used to set the value here ...
Burgee asked 8/10, 2019 at 2:53

13

Solved

What's the difference between initialization within a static block: public class staticTest { static String s; static int n; static double d; static { s = "I'm static"; n = 500; d = 4000....
Debi asked 21/2, 2012 at 14:38

1

Solved

//file1.cpp extern const char* foo; std::string bar = foo; //file2.cpp const char* foo = "foo"; Is bar guaranteed by the standard to be initialized to "foo"? Or could it be initialized before fo...
Vernita asked 12/8, 2019 at 11:10

1

Solved

A well known problem in C++ is the static initialization order fiasco. Is it still considered a problem when one use C++17 static inline members? Here an example where a static inline member is us...
Melissamelisse asked 9/6, 2019 at 14:18

3

Solved

I was wondering if it is possible to ensure that a function is only called during the static initialization step of a program? As an example, say that I have some singleton class that contains a s...
Allelomorph asked 19/12, 2018 at 9:58

1

Solved

CAUTION: it is not a duplicate, please read topic сarefully https://stackoverflow.com/users/3448419/apangin quote: The real question is why the code sometimes works when it should not. The issu...
Umbrella asked 11/12, 2018 at 12:56

3

I'd like to initialize a static std::map where the value is not copyable. I'll call my class ValueClass. ValueClass has an std::unique_ptr as private member and I even ensure that ValueClass is not...
Sparoid asked 16/12, 2018 at 21:35

2

Solved

I'm using a C++ library that can be built as either a shared or a static library. This library uses a factory technique, where static objects register themselves when the program starts and the sta...
Emelinaemeline asked 22/1, 2011 at 12:58

8

Solved

This question made me question a practice I had been following for years. For thread-safe initialization of function-local static const objects I protect the actual construction of the object, bu...
Timekeeper asked 2/6, 2010 at 8:4

1

MSVC 2017 Community with -std=c++17 chokes on the following example: #include <iostream> struct TC { static TC const values[]; static TC const& A; static TC const& B; static TC ...
Imprint asked 18/5, 2018 at 1:56

1

Solved

cppreference says about std::atexit : The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee tha...

6

Solved

First, a puzzle: What does the following code print? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scal...
Figone asked 19/3, 2018 at 14:56

1

The abridged problem (Y) Suppose you need to know, from within a function, whether this function has been called as part of the initialization of a static object, or not. Is there a standard or pl...
Dupuis asked 22/11, 2017 at 16:35

5

Solved

When I tried to write something like this: public interface MyInterface { static { System.out.println("Hello!"); } } the compiler could not compile it. But when I wrote something like this: ...
Humanitarian asked 1/11, 2013 at 7:56

© 2022 - 2025 — McMap. All rights reserved.