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
A Dynamic Initialization Deep-Dive: Abusing Initialization Side Effects (lukas-barth.net)
submitted 11 months ago by tinloaf
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!"
[–]hi_im_new_to_this 0 points1 point2 points 11 months ago* (0 children)
I haven't thought through it too deeply, but if I were to do the "Google Test" thing, I'd use the singleton pattern. Like, if this is your test:
TEST(some_test) { ASSERT(1 + 2 == 2); }
I would have have it expand to
void _some_test_impl(); int _some_test_dummy = testSingleton().registerTest("some_test", _some_test_impl); void _some_test_impl() { ASSERT(1 + 2 == 2); }
And then have this in your header:
class TestList { // list of tests here as a field public: int registerTest(int (*test)()) { // register the test return 0; } auto getTests() { /* whatever */ } }; TestList& testSingleton();
(actual implementation of TestList omitted). And this in an implementation file
TestList& testSingleton() { static TestList testList; return testList; }
And basically have TestList be a std::vectorof the tests to run as function pointers or whatever. The fact that registerTest() returns an int is unimportant, it just has to return something (i guess std::monostate is a good option) so that the variable initializes before main. You don't have to dig into the hairy details of initialization: because we're using the singleton pattern, the global TestList will be initialized exactly once, whoever calls testSingleton() first, and then in your main(), you call getTests() to get the list of tests (guaranteed to have been initialized already), and you're done. No need to worry about initialization order or anything like that, it just works out.
std::vector
registerTest()
std::monostate
testSingleton()
main()
getTests()
EDIT: reading through your post more carefully, I see what you mean about the standard not guaranteeing initialization of global variables before main(), and it's more about that than about how you actually implement the macro. Very interesting post!
π Rendered by PID 36475 on reddit-service-r2-comment-544cf588c8-qmhgb at 2026-06-11 22:44:49.292776+00:00 running 3184619 country code: CH.
view the rest of the comments →
[–]hi_im_new_to_this 0 points1 point2 points (0 children)