Questions tagged [initialization-order]

34 questions
80
votes
6 answers

C++ static initialization order

When I use static variables in C++, I often end up wanting to initialize one variable passing another to its constructor. In other words, I want to create static instances that depend on each other. Within a single .cpp or .h file this is not a…
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
65
votes
4 answers

Is this self initialization valid?

I have this question, which i thought about earlier, but figured it's not trivial to answer int x = x + 1; int main() { return x; } My question is whether the behavior of the program is defined or undefined if it's valid at all. If it's defined,…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
34
votes
2 answers

Are functions calls in a constructor's initializer-list sequenced?

Consider: int f () { static int i = 0; return i++; } struct Test { int a, b; Test () : a(f()), b(f()) {} }; Test t; I know that a is initialized before b due to the order of their declaration in the struct. I also know that the…
21
votes
1 answer

Why C++ forces initialization of member variables to be in the order of the declaration

I know that in C++ the declaration of members in the class header defines the initialization order. Can you tell me why C++ choose this design? Are there any benefits to force the initialize order instead of following the initializer list?
Wei Li
  • 1,847
  • 2
  • 10
  • 10
16
votes
5 answers

C++: Construction and initialization order guarantees

I have some doubts about construction and initialization order guarantees in C++. For instance, the following code has four classes X, Y, Z and W. The main function instantiates an object of class X, which contains an object of class Y, and derives…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
10
votes
4 answers

Is it a good idea to use initialization sections for module registration?

I am looking for a good solution for a decentralized module registration. I do not want a single unit that uses all module units of the project, but I would rather like to let the module units register themselves. The only solution I can think of is…
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
9
votes
1 answer

Static initialization order of inline variables in single TU

I'm aware this question has been asked many times, but this seems to be a slightly different variation which I can't figure out. Consider the following code: #include struct TestValue; inline const TestValue* v_ptr = nullptr; struct…
8
votes
1 answer

Using cout in the constructor of a class that is included in another class as a static member

Following code #include struct A { A() { std::cout << std::endl; } }; struct B { static inline A a; }; int main() { } succeeds after compiling with gcc, but crashes with segmentation fault after compiling with…
tilin
  • 332
  • 1
  • 7
7
votes
1 answer

When is the destructor of a constinit object called?

Generally it is said that the destructors of static objects are called in the reverse order of the constructors. As I understand, constinit objects are initialized at compile time, so their destructors should be called after the destructors of…
Helmut Zeisel
  • 388
  • 1
  • 10
7
votes
1 answer

std::atexit ordering when called from a global object's constructor

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 that if registration of A was sequenced-before the…
6
votes
1 answer

When are inline variables in static storage initialized?

C++ standards (earlier than C++17, at least) have said this about initialization order. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
6
votes
2 answers

Ambiguity in initialization order of static variables

During my research into the best way to build a Singleton in C# I stumbled across the following article where there is a brief mention that in C++ "The C++ specification left some ambiguity around the initialization order of static…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
5
votes
1 answer

How to summon a `given` member?

Suppose that I have some typeclass trait FooBar[X] and an instance of FooBar[Int]: given intIsFooBar: FooBar[Int] = new FooBar {} Now, suppose that I have an interface Intf that has some member type A and also guarantees that there is a given…
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
4
votes
1 answer

Initializing companion object after inner objects

Let's say I want to create sealed class, filled with some objects. Then I want to create list of all such objects, so I create list in companion object: fun main() { println(Color.Blue) println(Color.allColors) } sealed class Color { …
Matej Drobnič
  • 981
  • 10
  • 18
4
votes
2 answers

Why do I get an NPE when a nested Enum references a parent static member in its constructor?

Conditions to recreate (as far as I can tell): nested enum references a parent static member nested class static member of parent class takes enum as an constructor argument to nested class enum is referenced by an external class before anything…
rymach
  • 113
  • 1
  • 7
1
2 3