Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules regarding initialization of non-local statics?

Tags:

c++

c++11

Suppose I have a class whose only purpose is the side-effects caused during construction of its objects (e.g., registering a class with a factory):

class SideEffectCauser { public:   SideEffectCauser() { /* code causing side-effects */ } }; 

Also suppose I'd like to have an object create such side-effects once for each of several translation units. For each such translation unit, I'd like to be able to just put an a SideEffectCauser object at namespace scope in the .cpp file, e.g.,

SideEffectCauser dummyGlobal; 

but 3.6.2/3 of the C++03 standard suggests that this object need not be constructed at all unless an object or function in the .cpp file is used, and articles such as this and online discussions such as this suggest that such objects are sometimes not initialized.

On the other hand, Is there a way to instantiate objects from a string holding their class name? has a solution that is claimed to work, and I note that it's based on using an object of a type like SideEffectCauser as a static data member, not as a global, e.g.,

class Holder {   static SideEffectHolder dummyInClass; };  SideEffectHolder Holder::dummyInClass; 

Both dummyGlobal and dummyInClass are non-local statics, but a closer look at 3.6.2/3 of the C++03 standard shows that that passage applies only to objects at namespace scope. I can't actually find anything in the C++03 standard that says when non-local statics at class scope are dynamically initialized, though 9.4.2/7 suggests that the same rules apply to them as to non-local statics at namespace scope.

Question 1: In C++03, is there any reason to believe that dummyInClass is any more likely to be initialized than dummyGlobal? Or may both go uninitialized if no functions or objects in the same translation unit are used?

Question 2: Does anything change in C++11? The wording in 3.6.2 and 9.4.2 is not the same as the C++03 versions, but, from what I can tell, there is no behavioral difference specified for the scenarios I describe above.

Question 3: Is there a reliable way to use objects of a class like SideEffectHolder outside a function body to force side-effects to take place?

like image 414
KnowItAllWannabe Avatar asked Dec 05 '12 02:12

KnowItAllWannabe


People also ask

How to initialize a function in cpp?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

How do you initialize a static object in C++?

C # in Telugu We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

Do we need to initialize local variables in C++?

In the low-level efficiency tradition of C and C++ alike, the compiler is often not required to initialize variables unless you do it explicitly (e.g., local variables, forgotten members omitted from constructor initializer lists). Do it explicitly. There are few reasons to ever leave a variable uninitialized.

What does it mean to initialize c++?

In computer programming, initialization (or initialisation) is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized.


1 Answers

I think the only reliable solution is to design this for specific compiler(s) and runtime. No standard covers the initialization of globals in a shared library which I think is the most intricate case, as this is much dependent on the loader and thus OS dependent.

Q1: No Q2: Not in any practical sense Q3: Not in a standard way

like image 97
Jojje Avatar answered Sep 29 '22 10:09

Jojje