static-order-fiasco

Nifty/Schwarz counter, standard compliant?

情到浓时终转凉″ 提交于 2019-11-28 23:20:20
I had a discussion this morning with a colleague about static variable initialization order. He mentioned the Nifty/Schwarz counter and I'm (sort of) puzzled. I understand how it works, but I'm not sure if this is, technically speaking, standard compliant. Suppose the 3 following files (the first two are copy-pasta'd from More C++ Idioms ): //Stream.hpp class StreamInitializer; class Stream { friend class StreamInitializer; public: Stream () { // Constructor must be called before use. } }; static class StreamInitializer { public: StreamInitializer (); ~StreamInitializer (); } initializer; /

Static initialization order fiasco

断了今生、忘了曾经 提交于 2019-11-28 12:07:13
In his "Thinking in C++" (Chapter 10) Eckel describes a technique that was pioneered by Jerry Schwarz to solve the fiasco. He says that if we want to initialize x to 100 and y to 200 and share them among all translation units, we create an Initializer.h that looks like this: extern int x; extern int y; class Initializer { static int initCount; // if (initCount++ == 0) x = 100 & y = 200 /* ... */ }; static Initializer init; And in implementation file we have #include "Initializer.h" int x; int y; int Initializer::initCount; and Eckel says that "static initialization (in implementation file)

Nifty/Schwarz counter, standard compliant?

坚强是说给别人听的谎言 提交于 2019-11-27 14:53:35
问题 I had a discussion this morning with a colleague about static variable initialization order. He mentioned the Nifty/Schwarz counter and I'm (sort of) puzzled. I understand how it works, but I'm not sure if this is, technically speaking, standard compliant. Suppose the 3 following files (the first two are copy-pasta'd from More C++ Idioms): //Stream.hpp class StreamInitializer; class Stream { friend class StreamInitializer; public: Stream () { // Constructor must be called before use. } };

Prevent static initialization order “fiasco”, C++

回眸只為那壹抹淺笑 提交于 2019-11-27 07:41:11
问题 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 functions, thus to prevent the "fiasco" by maintaining the creation order of variables. But this seems to me a rude workaround. So my question is, is there any modern, more pattern oriented way to prevent this "fiasco" but to wrap the "static stuff" into functions??? 回答1: The modern, more pattern

Static initialization order fiasco

大城市里の小女人 提交于 2019-11-27 06:45:44
问题 In his "Thinking in C++" (Chapter 10) Eckel describes a technique that was pioneered by Jerry Schwarz to solve the fiasco. He says that if we want to initialize x to 100 and y to 200 and share them among all translation units, we create an Initializer.h that looks like this: extern int x; extern int y; class Initializer { static int initCount; // if (initCount++ == 0) x = 100 & y = 200 /* ... */ }; static Initializer init; And in implementation file we have #include "Initializer.h" int x; int

static initialization order fiasco

本秂侑毒 提交于 2019-11-26 03:16:20
问题 I was reading about SIOF from a book and it gave an example : //file1.cpp extern int y; int x=y+1; //file2.cpp extern int x; int y=x+1; Now My question is : In above code, will following things happen ? while compiling file1.cpp, compiler leaves y as it is i.e doesn\'t allocate storage for it. compiler allocates storage for x, but doesn\'t initialize it. While compiling file2.cpp, compiler leaves x as it is i.e doesn\'t allocate storage for it. compiler allocates storage for y, but doesn\'t

C++ static initialization order

只愿长相守 提交于 2019-11-26 01:39:48
问题 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 problem: the instances will be created in the order they are declared. However, when you want to initialize a static instance with an instance in another compilation unit, the order seems impossible to specify. The result is that, depending on the

Finding C++ static initialization order problems

╄→尐↘猪︶ㄣ 提交于 2019-11-25 23:27:33
问题 We\'ve run into some problems with the static initialization order fiasco, and I\'m looking for ways to comb through a whole lot of code to find possible occurrences. Any suggestions on how to do this efficiently? Edit: I\'m getting some good answers on how to SOLVE the static initialization order problem, but that\'s not really my question. I\'d like to know how to FIND objects that are subject to this problem. Evan\'s answer seems to be the best so far in this regard; I don\'t think we can