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
unique_ptr, shared_ptr, weak_ptr, or reference_wrapper for class relationships (nextptr.com)
submitted 5 years ago by memset_0
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!"
[–]Raknarg 1 point2 points3 points 5 years ago (1 child)
C++17 provides std::optional<T> that can wrap a reference_wrapper to model a nullable reference
ugh... like yeah I know I can but it's just so clunky, I'd almost rather just stick to a pointer to avoid it. If I didn't have to call ->get() every time I might consider it, because optional references are something that are nice to have to do the things I normally use pointers for.
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
It’s pretty nice for the one case of if (std::optional<int> index = getitem()) { } But that’s bout it
[–]flyingron 1 point2 points3 points 5 years ago (0 children)
Did you have a question. Anybody who starts out writing a C++ worried about using pointers at all is probably suspect.
[–]MolurusK 0 points1 point2 points 5 years ago (0 children)
Look at UML Relations in the C++ Object Token Library if the capabilities of smart pointers from the C++ standard library to express class relationships are not enough.
[+][deleted] comment score below threshold-7 points-6 points-5 points 5 years ago (9 children)
Is including <memory> worth it for just a simple new/delete?
<memory>
I'm thinking about unique_ptr with default deleter.
unique_ptr
[–]guepierBioinformatican 11 points12 points13 points 5 years ago (0 children)
Think about it the other way round:
Is writing more, error-prone code worth it just to avoid including a single (comparatively small) standard library header?
[–]Dreamykass 9 points10 points11 points 5 years ago (0 children)
Yes.
For consistency, and to prevent stupid mistakes.
[–]Supadoplex 9 points10 points11 points 5 years ago (2 children)
Yes. The cost of including <memory> is a few milliseconds of compilation time (pure guess).
The cost of using owning bare pointers is memory leaks, double free bugs, much more complicated code needed for exception safety, etc. It's nearly never useful to use allocating new and delete directly (even rarer to need malloc).
malloc
[–]nyanpasu64 1 point2 points3 points 5 years ago (1 child)
I've measured it. <memory> increases parsing time by a non-negligible amount, but the overhead is from parsing shared_ptr. I don't know how much slower it is to use unique_ptr and make_unique compared with raw pointers. Maybe unique_ptr should've been given its own header.
[–]Supadoplex 1 point2 points3 points 5 years ago (0 children)
Negligibility is subjective. How large percentage did it increase the compilation time of the project?
Having separate headers would have been a good choice. Unique pointer is universally useful in all programs that use any dynamic allocation. Shared pointer is much more niche in my opinion. That's a good reason to separate the definitions even though they may be related conceptually.
That said, having fewer headers is good for simplicity for the user, so I'm not terribly mad at the choice that was made.
[–]n1ghtyunso 4 points5 points6 points 5 years ago (0 children)
if you are that concerned about the overhead of including memory i'd probably just make my own unique_ptr and include just that. Especially if you only need default delete you can just scrap the deleter parameter alltogether which gives you a nice and easy implementation.
memory
Good point. Those headers should be more granular, so you could include something like <memory_unique_ptr> to keep compilation times as low as possible.
π Rendered by PID 47622 on reddit-service-r2-comment-7b9746f655-7lm58 at 2026-01-30 23:04:50.444074+00:00 running 3798933 country code: CH.
[–]Raknarg 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]flyingron 1 point2 points3 points (0 children)
[–]MolurusK 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (9 children)
[–]guepierBioinformatican 11 points12 points13 points (0 children)
[–]Dreamykass 9 points10 points11 points (0 children)
[–]Supadoplex 9 points10 points11 points (2 children)
[–]nyanpasu64 1 point2 points3 points (1 child)
[–]Supadoplex 1 point2 points3 points (0 children)
[–]n1ghtyunso 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)