Questions tagged [static-initialization]

Questions regarding initialization code of static members

325 questions
7
votes
3 answers

Is it possible to ensure that a function is only called during the 'static initialization' step

I was wondering if it is possible to ensure that a function is only called during the static initialization step of a program? As an example, say that I have some singleton class that contains a std::map object and exposes the insert and at methods…
Jon Burr
  • 97
  • 7
7
votes
1 answer

std::atexit ordering when called from a global object's constructor

cppreference says about std::atexit : The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee that if registration of A was sequenced-before the…
7
votes
2 answers

Why is the order of destruction of these function-local static objects NOT the inverse of their order of initialization?

I have two function-local static objects, One and Two. One's constructor and destructor both access Two through GetTwo(): #include struct One; struct Two; const One& GetOne(); const Two& GetTwo(); struct Two { const char* value =…
user3217070
7
votes
2 answers

Is initialization of local static function-object thread-safe?

The following two functions produce different assemblies, which tells me they're different. Can someone tell me in what way they are different? And is the function local static variable initialization in func2 thread-safe or not? If the answer…
zeroes00
  • 531
  • 3
  • 16
6
votes
0 answers

Why does clang do thread-safe init for some globals, but not others?

Consider a global (namespace scope) variable declared using the new inline variable feature in C++ 17: struct something { something(); ~something(); }; inline something global; In Clang 14 on x86 the generated assembly to initialize the…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
6
votes
1 answer

How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

Ben Voigt has pointed out here that: Zero initialization is one of the steps of static initialization. But you're right that you can't blindly substitute the latter (tag), since zero initialization is also performed for value initialization.…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
1 answer

Legitimate uses for static initializer?

I remember a couple years ago I was using static initializers to call class-level setup operations. I remember it having very bizarre behaviors and I just decided to steer clear from them. Maybe it was because I was messing up the top-bottom order…
tmn
  • 11,121
  • 15
  • 56
  • 112
6
votes
2 answers

Indirect recursion, dependent static variables

Is the result of the following indirect recursion defined by the standard or is it undefined behavior? auto abc() -> int ; auto xyz() -> int { static int instance = 3 + abc(); return instance; } auto abc() -> int { static int instance…
MFH
  • 1,664
  • 3
  • 18
  • 38
6
votes
2 answers

Thread-safety of static initializers in C#

Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { …
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
2 answers

Default value for struct parameter

Let's say I have the following struct: struct myStruct { int x; int y; int z; int w; }; I want to initialize this struct to a default value when calling the following function. If it helps I'm looking for a simple zero initialization. void…
atoMerz
  • 7,534
  • 16
  • 61
  • 101
6
votes
4 answers

How to circumvent the size limit of a static initialiser in Java when initialising large amounts of constants

I have a class holding a large a mount of generated constants as such: public class Constants extends SomeBaseClass { // init() is defined in some base class... public static final XXX KEY1 = init(...); public static final XXX KEY2 =…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
5
votes
2 answers

Is the order of file-level static variables always the same within a given translation unit?

I have a program split up into two source files: example.cpp #include class A { public: A(int x) { ::std::cout << "In A(" << x << ")\n"; } }; static A first(1); static A second(2); example__main.cpp int main(int argc,…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
5
votes
2 answers

C++ is it possible to delay initialization of constant static member?

I am using Qt but this is a generic C++ question. My case is simple, I have a class Constants which has a constant static member which I want it to be initialized after certain function calls are made. Constants.h #ifndef CONSTANTS_H #define…
destan
  • 4,301
  • 3
  • 35
  • 62
5
votes
2 answers

Can "construct on first use" idiom fail under any circumstances?

I'm building my program (tests actually) using some static library. This library contains one file inside which I have functions like that: string& GetString() { static string strFilename; return strFilename; } void PrintToScreen() { …
Piotr Kukielka
  • 3,792
  • 3
  • 32
  • 40
5
votes
1 answer

Is a pointer to string literal guaranteed to be initialized before a std::string?

//file1.cpp extern const char* foo; std::string bar = foo; //file2.cpp const char* foo = "foo"; Is bar guaranteed by the standard to be initialized to "foo"? Or could it be initialized before foo gets set and segfault in the constructor i.e. a…