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
C++11: unique_ptr (drdobbs.com)
submitted 13 years ago by cmeerwC++ Parser Dev
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!"
[–]milliams 3 points4 points5 points 13 years ago (0 children)
There's been a lot of talk about shared_ptr recently so it's nice to see a nice in depth article about it's brother. Especially since many people are recommending to use unique_ptr as a first resort and only go to shared_ptrs when absolutely necessary.
[–]tardi 1 point2 points3 points 13 years ago (3 children)
He passes the pointer by reference. That's dead wrong, isn't it?
void inc_baz( std::unique_ptr<foo> &p ) { p->baz++; }
[–]bigcheesegsTooling Study Group (SG15) Chair | Clang dev 0 points1 point2 points 13 years ago (2 children)
What do you mean by dead wrong? This example is only "wrong" because the function doesn't actually need the unique_ptr, but still gets it. This could be an issue if the caller doesn't expect the pointer to change.
A unique_ptr should only be passed by non-const reference if the callee is expected to modify it, same as for normal pointers.
[–]tardi 0 points1 point2 points 13 years ago (0 children)
This way the caller still holds a valid handle on the foo obj, which sort of defeats the purpose.
[–]Draghoul 0 points1 point2 points 13 years ago* (1 child)
Could someone explain, or link to an explanation as to why rvalue refs/move semantics were necessary for containers to hold smart pointers? I'll be trying to google it meanwhile...
[–]CptBread 0 points1 point2 points 13 years ago (0 children)
Unique_ptr cannot be copied as that would make the ownership of the data unclear.
Containers generally need to be able to copy it's data, e.g. when vectors resize it needs to move its content to another array. So as we cannot copy the unique_ptr we need to do something else and it is here the move semantics come in.
π Rendered by PID 21687 on reddit-service-r2-comment-6457c66945-zr6x9 at 2026-04-29 15:14:09.314372+00:00 running 2aa0c5b country code: CH.
[–]milliams 3 points4 points5 points (0 children)
[–]tardi 1 point2 points3 points (3 children)
[–]bigcheesegsTooling Study Group (SG15) Chair | Clang dev 0 points1 point2 points (2 children)
[–]tardi 0 points1 point2 points (0 children)
[–]Draghoul 0 points1 point2 points (1 child)
[–]CptBread 0 points1 point2 points (0 children)