In fact, this does not guarantee. One determination problem is solved by simply defining the flow objects once in the .cpp file, which is part of the library. The code in the question simply contains a declaration of standard threads.
Which is a guarantee that objects will be initialized before use. One problem with global objects in C ++ is that although they are initialized in order in each .cpp file, we do not know the order in which the linker will place objects from separate files. This can cause problems if one object tries to use another object before it is initialized, and we donβt know the exact order - see Fixing the order of static initialization .
One workaround used here is to add an Init object to the header declaring the stream objects. Since you must include the header before using the stream, we know that this Init object will be at the top of the file and, therefore, created in front of other objects that the streams can use.
Now the constructor of the Init class is responsible for initializing the stream objects. This must be done early and only once. Itβs exactly the same as before each implementation, but the code hints at the use of a counter that tracks the number of Init objects created (and, apparently, considering the first specifically).
This is one way to do this using only the standard language. Some implementations have other tricks, such as # pragma or init_priority , to convince the linker to put the library code in front of the user code. In this case, it just works by magic without using the Init class.
source share