storage-duration

c++ static vs thread storage duration destruction order

北慕城南 提交于 2019-12-24 03:32:12
问题 Consider how in c++ there are these two storage duration (among others): static storage duration and thread storage duration.. Next consider this code: static MyClassA a; thread_local static MyClassB b; Additional pretend that "a" and "b" might not be in the same compilation unit. I "believe" that the destructor of "b" will be called before "a" as the thread storage duration will terminate first and only after that is complete will the static storage duration terminate and call the destructor

Does a reference have a storage location?

一个人想着一个人 提交于 2019-12-18 18:40:56
问题 Does a reference have a storage location or is it just an alias for another location? Does this differ by C++ revision or is it consistent with all versions of C++? And if a reference has a storage location, does it then just allow value semantics on a pointer like type? How would a reference work when you use it as such: struct aStruct{ int aVariable; aClass& aReferencetoaClass; }; Does it take up space or is it an alias? 回答1: The latest C++20 spec(§ 9.2.3.3) and at least since the C++ 2005

Initialization of static data member

旧街凉风 提交于 2019-12-13 04:23:36
问题 Why does default initialization of static data member not occur? In the following example struct data_member { data_member(){ cout << "data_member\n"; } ~data_member(){ cout << "~data_member\n"; } }; struct Y { static data_member m; Y(){ cout << "Y\n"; } ~Y(){ cout << "~Y\n"; } }; Y y; //call constructor of Y but if we delete static specifier from data_member m it will be default-initialized. struct data_member { data_member(){ cout << "data_member\n"; } ~data_member(){ cout << "~data_member

Does C++ default-initialization preserve prior zero-initialization?

邮差的信 提交于 2019-12-01 04:03:38
If a C++ constructor for an object with static-storage duration does not initialize a member, is that required to preserve the prior zero-initialization, or does it leave the member with an indeterminate value? My reading of the C++ spec is that it contradicts itself. Example: #include <iostream> struct Foo { Foo(); int x; } object; Foo::Foo() { } int main() { std::cout << object.x << std::endl; } The Foo() constructor does not explicitly initialize the member object.x, so according to the note in 12.6.2 paragraph 8: the member has indeterminate value. But working through the details of the

Does C++ default-initialization preserve prior zero-initialization?

时间秒杀一切 提交于 2019-12-01 01:06:19
问题 If a C++ constructor for an object with static-storage duration does not initialize a member, is that required to preserve the prior zero-initialization, or does it leave the member with an indeterminate value? My reading of the C++ spec is that it contradicts itself. Example: #include <iostream> struct Foo { Foo(); int x; } object; Foo::Foo() { } int main() { std::cout << object.x << std::endl; } The Foo() constructor does not explicitly initialize the member object.x, so according to the