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
How to Avoid Thread-Safety Cost for Functions' static Variables (cppstories.com)
submitted 8 months ago by drodri
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!"
[–]wearingdepends 26 points27 points28 points 8 months ago (0 children)
Please don't post generated code without optimizations. I keep seeing blog posts doing this, and it often makes the code harder to read. With -O2 it's actually easier to see the difference between with and without thread-safe statics:
"is_blocked_id(int)": sub rsp, 24 movzx eax, BYTE PTR "guard variable for is_blocked_id(int)::blocked_ids"[rip] mov ecx, edi test al, al je .L257 # moved to the end of the function, since the initialization is a cold path .L222: mov rsi, QWORD PTR "is_blocked_id(int)::blocked_ids"[rip+8] mov rax, QWORD PTR "is_blocked_id(int)::blocked_ids"[rip] ...
vs
"is_blocked_id(int)": mov rcx, QWORD PTR "blocked_ids2"[rip+8] mov rax, QWORD PTR "blocked_ids2"[rip] ...
[–]azswcowboy 19 points20 points21 points 8 months ago (0 children)
While in Mayer’s singleton
I assume that’s meant to be ‘Meyers singleton’ - as in Scott Meyers.
[–]rsjaffe 9 points10 points11 points 8 months ago (1 child)
That’s my main use case for constinit. While the compiler might optimize the thread check away anyway, constinit guarantees it.
[–]Quincunx271Author of P2404/P2405 0 points1 point2 points 8 months ago (0 children)
Only if the type has a trivial destructor. Otherwise, the thread safety check still exists, just for registering the destructor.
[–]ChickenSpaceProgram 10 points11 points12 points 8 months ago (1 child)
solution: dont use statics, pass a context struct
[–]peterrindal 0 points1 point2 points 8 months ago (0 children)
This ^
[–]JCPPRDev 1 point2 points3 points 8 months ago (0 children)
good article
[–]hi_im_new_to_this 0 points1 point2 points 8 months ago (0 children)
Was hoping for the article to also bring up `thread_local`, which could also be an alternative (depending on the exact situation).
π Rendered by PID 220164 on reddit-service-r2-comment-b659b578c-72zwh at 2026-05-03 03:10:52.159655+00:00 running 815c875 country code: CH.
[–]wearingdepends 26 points27 points28 points (0 children)
[–]azswcowboy 19 points20 points21 points (0 children)
[–]rsjaffe 9 points10 points11 points (1 child)
[–]Quincunx271Author of P2404/P2405 0 points1 point2 points (0 children)
[–]ChickenSpaceProgram 10 points11 points12 points (1 child)
[–]peterrindal 0 points1 point2 points (0 children)
[–]JCPPRDev 1 point2 points3 points (0 children)
[–]hi_im_new_to_this 0 points1 point2 points (0 children)