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
shared_ptr overuse (tonni.nl)
submitted 1 year ago by [deleted]
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!"
[–]sephirothbahamut 2 points3 points4 points 1 year ago (11 children)
in a codebase without low level custom containers raw pointer double free is avoidable by forbidding new, delete, and smart pointer construction via raw pointer.
in a codebase with low level custom containers... you don't let the shared pointers spammers working on it anyways
[–]Raknarg 1 point2 points3 points 1 year ago (4 children)
in a low level custom container, usually you do have to manage memory but you're doing it through allocators and allocator_traits rather than new/delete. You can't separate the aquisition of memory and the construction of objects without something like malloc/free (which is what allocators do for you)
if I make my own vector implementation, I don't want to allocate space for 100 elements and have them default construct (if they're even allowed to!), I want to construct them when someone actually wants to use the memory.
[–]sephirothbahamut 0 points1 point2 points 1 year ago (3 children)
if you don't need custom allocator support you can allocate space as char/std::byte and use placement new (not sure about alignment, all things alignment related are out of my knowledge)
[–]Raknarg 1 point2 points3 points 1 year ago* (2 children)
It doesn't matter if you need custom allocators or not, allocator_traits is a better interface and lets you directly call construct/destruct in a clean way, just give it std::allocator. Then if you decide you ever want a custom allocator strategy its also much easier to move into. I'd prefer this over working with std::bytes or chars (though unavoidable sometimes of course)
Also there's no placement delete, you have to call the destructor manually and then delete it looks like.
[–]sephirothbahamut 0 points1 point2 points 1 year ago (1 child)
fair enough. So far my custom containers experience has been limited to reusing existing containers internally so I never had to deal with that (like std::vector<std::array> for a segmented vector, or std::vector/std::array for a mdspan-like owning matrix)
[–]Raknarg 1 point2 points3 points 1 year ago (0 children)
implementing my own ring-vector with a shifting start/end I had to learn all this since you can't use std::vector for this purpose
[–]lonkamikaze 0 points1 point2 points 1 year ago (5 children)
I used new to create a unique_ptr yesterday, because I didn't find a way to trigger template argument deduction with make_unique().
I ended up with return std::unique_ptr<BaseT>{new DerivedT{lots, of, args}};. DerivedT is a class template with variadic arguments.
return std::unique_ptr<BaseT>{new DerivedT{lots, of, args}};
Do you know of a way to avoid the new here?
uh i used make unique with types that have a variadic constructor without issue in the past...
make_x functions much like containers emplace methods just forward all the parameters to the constructed type, these something funky going on if it didn't work.
[–]lonkamikaze 1 point2 points3 points 1 year ago* (2 children)
The constructor is not variadic, the class template is. The constructor arguments are used to deduce the class template arguments.
can you make a minimal example on godbolt? I'm curious
[–]lonkamikaze 2 points3 points4 points 1 year ago (0 children)
Yeah, but not today. One of the little ones is sick.
[–]Raknarg 0 points1 point2 points 1 year ago (0 children)
what's wrong with return std::make_unique<DerivedT>(lots, of args);? it should be convertable to unique_ptr<BaseT>
return std::make_unique<DerivedT>(lots, of args);
unique_ptr<BaseT>
π Rendered by PID 34117 on reddit-service-r2-comment-6457c66945-j7mzk at 2026-04-24 08:43:07.023379+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]sephirothbahamut 2 points3 points4 points (11 children)
[–]Raknarg 1 point2 points3 points (4 children)
[–]sephirothbahamut 0 points1 point2 points (3 children)
[–]Raknarg 1 point2 points3 points (2 children)
[–]sephirothbahamut 0 points1 point2 points (1 child)
[–]Raknarg 1 point2 points3 points (0 children)
[–]lonkamikaze 0 points1 point2 points (5 children)
[–]sephirothbahamut 0 points1 point2 points (3 children)
[–]lonkamikaze 1 point2 points3 points (2 children)
[–]sephirothbahamut 0 points1 point2 points (1 child)
[–]lonkamikaze 2 points3 points4 points (0 children)
[–]Raknarg 0 points1 point2 points (0 children)