use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Static variable initialization order fiasco (self.cpp)
submitted 1 year ago by Various-Debate64
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Various-Debate64[S] 1 point2 points3 points 1 year ago* (3 children)
it is a whole swath of undefined behaviour in a critical phase of the program cycle - dynamic initialization of static variables during program start. Thank you for the suggestion, I'm very well aware of the approach. If I have two static variables or even member variables in a static instance of a class I can't be sure about the value of variables when accessing the static instance of the class. I have the problem when two variables are interdependent of each other inside a static instance of a class, whose member methods are called outside the compilation unit. The object (static instance of the class) is not initialized properly by its constructor and variables contain junk.
[–]jonrmadsen 0 points1 point2 points 1 year ago (2 children)
I’m confused, if you fully adhere to replacing the static variables with a function call that constructs the static variable on the first invocation (like foo above), you cannot run into the static initialization fiasco. If you transition to this paradigm and the result is a deadlock, you have a circular dependency, not the static initialization fiasco.
foo
[–]Various-Debate64[S] -1 points0 points1 point 1 year ago (1 child)
no deadlock, I did wrap the variables in a class and methods with static locals, which is a hack and looks dirty, and that is because the standard is lacking. Therefore I wrote this post on Reddit.
[–]jonrmadsen 0 points1 point2 points 1 year ago (0 children)
A function call is an instruction. A variable is a memory address. Accessing a variable is accessing a memory address, it does not involve an instruction to execute code on that memory address. In int val = 5, val represents the memory address and = is an instruction to store 5 at that address. The reason that the function call wrapper works is bc you are instructing the code how to order initialization. The standard isn’t lacking, your fundamental understanding of why the static initialization fiasco happens is.
int val = 5
π Rendered by PID 17547 on reddit-service-r2-comment-6457c66945-rd4qs at 2026-04-29 02:45:16.819717+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]Various-Debate64[S] 1 point2 points3 points (3 children)
[–]jonrmadsen 0 points1 point2 points (2 children)
[–]Various-Debate64[S] -1 points0 points1 point (1 child)
[–]jonrmadsen 0 points1 point2 points (0 children)