initialization-order

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

杀马特。学长 韩版系。学妹 提交于 2020-07-06 10:52:13
问题 Following code #include <iostream> 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 clang. Is the code not standard or is clang wrong? https://godbolt.org/z/tEvfrW 回答1: Cppreference on std::ios_base::Init reads: The header <iostream> behaves as if it defines (directly or indirectly) an instance of std::ios_base::Init with static storage duration: this makes

Using a free “char const*” at static initialization time

三世轮回 提交于 2019-12-11 10:43:48
问题 Initialization order of free objects is undefined in C++. But what about the following? namespace foo { char const* str = "hey"; struct A { A() { cout << str; } } obj; } Is this still undefined behavior, or is there a special provision for pointers initialized with string literals? Aside from that : what if str was of type "char const[]"? And if it was a std::string? 回答1: Even if they would be located in different translation units, the initialisation order is still defined. That is because

Initialization order issues

孤者浪人 提交于 2019-12-07 15:50:33
问题 Given the code sample: class B { //Some contents. }; class C { private: B& b; }; class A { private: B b; C c; }; Class C has a reference to a b, so it needs to be initialized with it. Class A contains an instance of B and an instance of C. My question is: Can I initialize the C instance in A with the B instance in A (assuming I did bother to put the constructors in)? Secondly, do I need to perform any explicit initialization of the B in A, or is it default initialized since its a class type

Initialization order issues

泄露秘密 提交于 2019-12-05 23:41:22
Given the code sample: class B { //Some contents. }; class C { private: B& b; }; class A { private: B b; C c; }; Class C has a reference to a b, so it needs to be initialized with it. Class A contains an instance of B and an instance of C. My question is: Can I initialize the C instance in A with the B instance in A (assuming I did bother to put the constructors in)? Secondly, do I need to perform any explicit initialization of the B in A, or is it default initialized since its a class type within a class? Seth Carnegie Member variables are initialised in the order that they are declared in

Is this self initialization valid?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 04:47:12
问题 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, is the value of x known in main ? 回答1: I'm pretty sure it's defined, and x should have the value 1. §3.6.2/1 says: "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place." After

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