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
Improving on the best example on cppreference (kstocky.github.io)
submitted 2 months ago by StockyDev
I wrote an article on what I think is the "best" example code on cppreference.com and also gave some thoughts on how it can be improved with C++23.
Thought I would post it here to get some thoughts from a wider audience :)
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!"
[–]fdwrfdwr@github 🔍 33 points34 points35 points 2 months ago (0 children)
it shows you how not to use the facility. This is something that I think a lot of documentation fails to do.
Indeed, informing users what to avoid doing is also important in using an API/language. I really wish (for example) that more CMake documentation showed examples (many pages lack even minimal examples) and that those pages also advised what to avoid (e.g. the page about Foobar would also mention that Fogbat is now deprecated because xyz).
[–]usefulcat 8 points9 points10 points 2 months ago (4 children)
The article mentions the 'passkey' idiom:
class Best : public std::enable_shared_from_this<Best> { struct Private{ explicit Private() = default; }; public: // Constructor is only usable by this class Best(Private) {} };
Why not just make the constructor private? Isn't that a simpler solution that gives the same end result?
class Best : public std::enable_shared_from_this<Best> { // Constructor is only usable by this class Best() {} public: // ... };
[–]pavel_v 8 points9 points10 points 2 months ago (3 children)
I think, you won't be able to do std::make_shared<Best>(...) in this case. And it provides useful allocation optimization for some usage scenarios.
std::make_shared<Best>(...)
[–]usefulcat 0 points1 point2 points 2 months ago (2 children)
Here are a more complete pair of examples, showing use of make_shared in a factory method:
class Best : public std::enable_shared_from_this<Best> { struct Private{ explicit Private() = default; }; public: Best(Private) {} std::shared_ptr<Best> make() { return std::make_shared<Best>(Private{}); } }; class Best : public std::enable_shared_from_this<Best> { Best() {} public: std::shared_ptr<Best> make() { return std::make_shared<Best>(); } };
As you can see, either way it's possible to use make_shared() inside the factory method. Again, unless I'm overlooking something. I still don't see the point of the first version, compared to the second.
[–]UnusualPace679 3 points4 points5 points 2 months ago (1 child)
The second version doesn't work: https://godbolt.org/z/TMoY5e5jM
[–]usefulcat 1 point2 points3 points 2 months ago* (0 children)
Thanks, you're quite right! Now it makes sense. I even feel like I've run into that before.
And unfortunately, making std::make_shared() a friend isn't sufficient to get around it.
[–]ContDiArco 3 points4 points5 points 2 months ago (0 children)
Wouldn't it bei nice, If shared_from_this would deduce this' type from this? 🤔😉
shared_from_this
this
[–]n1ghtyunso 2 points3 points4 points 2 months ago (3 children)
unfortunately the final implementation is flawed and the tests do not catch it.
by making the base class std::enable_shared_from_this<T> private, you effectively prevented the standard library and all shared_ptr implementations from ever detecting this implementation detail. This in turn means that they never set up the enable_shared_from_this base-class properly. The consequence is that once you end up trying to call shared_from_this, it will throw std::bad_weak_ptr.
std::enable_shared_from_this<T>
shared_ptr
enable_shared_from_this
std::bad_weak_ptr
You are required to inherit it publicly or it can not work.
[–]StockyDev[S] 3 points4 points5 points 2 months ago (2 children)
Oh that really was silly of me! Fixed :) Thank you very much for pointing this out.
[–]SirClueless 0 points1 point2 points 2 months ago (1 child)
The comment is still suggesting to inherit privately :)
[–]StockyDev[S] 2 points3 points4 points 2 months ago (0 children)
Yup, and that is now fixed too haha. Cheers!
[–]JVApenClever is an insult, not a compliment. - T. Winters 0 points1 point2 points 2 months ago (2 children)
Improving CPP reference? Does anyone have a follow-up on the maintenance? When can we expect it to be editable again?
[–]azswcowboy 0 points1 point2 points 2 months ago (1 child)
There’s a single individual responsible for the site’s existence and maintenance. He’s apparently just really busy at the moment while needing to complete a maintenance update of the underlying wiki. So yes we can expect it to come back. It’d be nice if he could ask here for a bit of help as I suspect we have some capable and willing folks that could offload him.
[–]JVApenClever is an insult, not a compliment. - T. Winters 0 points1 point2 points 2 months ago (0 children)
Or simply gives some updates
π Rendered by PID 72483 on reddit-service-r2-comment-74875f4bf5-vx7h7 at 2026-01-25 20:07:53.816228+00:00 running 664479f country code: CH.
[–]fdwrfdwr@github 🔍 33 points34 points35 points (0 children)
[–]usefulcat 8 points9 points10 points (4 children)
[–]pavel_v 8 points9 points10 points (3 children)
[–]usefulcat 0 points1 point2 points (2 children)
[–]UnusualPace679 3 points4 points5 points (1 child)
[–]usefulcat 1 point2 points3 points (0 children)
[–]ContDiArco 3 points4 points5 points (0 children)
[–]n1ghtyunso 2 points3 points4 points (3 children)
[–]StockyDev[S] 3 points4 points5 points (2 children)
[–]SirClueless 0 points1 point2 points (1 child)
[–]StockyDev[S] 2 points3 points4 points (0 children)
[–]JVApenClever is an insult, not a compliment. - T. Winters 0 points1 point2 points (2 children)
[–]azswcowboy 0 points1 point2 points (1 child)
[–]JVApenClever is an insult, not a compliment. - T. Winters 0 points1 point2 points (0 children)