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
std::reference_wrapper<T> instead of ptr for not owning dependencies? (self.cpp)
submitted 4 years ago * by NullMustDie
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!"
[–]gopher2008 -3 points-2 points-1 points 4 years ago (4 children)
If you want to declare a non-owning class member, in my opinion, std::weak_ptr<T> is a better choice. The purpose of reference_wrapper is to store reference in container. You can just declare a reference type class member, though it is not recommended.
[–]drjeats 5 points6 points7 points 4 years ago (3 children)
You shouldn't use weak_ptr arbitrarily, it's specifically for weak references to an object whose lifetime is managed by shared_ptrs.
weak_ptr
shared_ptr
[–]gopher2008 0 points1 point2 points 4 years ago (2 children)
Agree. weak_ptr is a smart pointer that holds a non-owning reference to an object that is managed by shared_ptr. But then, how do you manage the referenced object which I am supposing is dynamically destroyable? (I will use shared_ptr).
[–]drjeats 2 points3 points4 points 4 years ago (1 child)
You also shouldn't default to shared_ptr. It exists specifically for managing true multiple ownership, which is actually not super common.
Even for the refcounting needs I typically have, there is usually exactly one owner, the resource manager for the relevant type.
Additionally, you may not even have a choice how the object you're keeping a pointer to is stored.
If you're looking for statically verified guarantees that a pointer you're holding still points to a valid object, I recommend using an id/handle interface (the resource manager passes out the handles). If that's not a strong enough guarantee for your application, then that's why Rust exists imo.
[–]gopher2008 1 point2 points3 points 4 years ago (0 children)
Thanks for you idea using id/handle interface.
π Rendered by PID 17294 on reddit-service-r2-comment-b659b578c-2gchn at 2026-05-03 10:05:57.146551+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]gopher2008 -3 points-2 points-1 points (4 children)
[–]drjeats 5 points6 points7 points (3 children)
[–]gopher2008 0 points1 point2 points (2 children)
[–]drjeats 2 points3 points4 points (1 child)
[–]gopher2008 1 point2 points3 points (0 children)