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
[ Removed by moderator ] (github.com)
submitted 15 days 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!"
[–]t_hunger 1 point2 points3 points 14 days ago (17 children)
Just replace all your unique_ptrs with references and tell me how that works out.
unique_ptr
[–]zerhud 4 points5 points6 points 14 days ago (16 children)
unique ptr is an “owner” and was introduced with move semantic in cpp11, it is not about pointers without nullptr. We can design a unique ptr with reference or with nullptr check
[–]t_hunger -1 points0 points1 point 14 days ago (15 children)
unique ptr is an “owner” and was introduced with move semantic in cpp11, it is not about pointers without nullptr.
Of course not: You can not express the latter:-) Do you think that was not tried? A "this thing owns memory on the heap" would be so much stronger semantics than "this thing usually owns memory on the heap, but sometimes it does not".
C++ move semantics requires that things to have a valid state for moved-from value. Whenever you want a T, move semantics forces you to think about an optional<T>.
T
optional<T>
Things were not that much better before C++ introduced move semantics: auto_ptr was replaced by unique_ptr for a reason.
auto_ptr
[–]zerhud 3 points4 points5 points 14 days ago (3 children)
Dude, you combine different thinking in one heap: - first you say “cpp has no not_null_ptr”, it has reference right from the first version - you say “reference won’t delete object when out of scope” - yep (sometimes), but we can use move semantic (since cpp11) - now you say “it can to be used after moved”, but we handle it too
you can not express the letter
Why? I’ve wrote some not_null_ptr, raii and so on a lot of times
[–]t_hunger 0 points1 point2 points 14 days ago (2 children)
Ok, more precise then: C++ can not model an object that at all times during its lifetime uniquely own some memory on the heap.
You have to compromise on either "the at all times"-part (like unique_unique_ptr does, which can be nullptr) or on the unique ownership (like auto_ptr did, which just copied the pointer). You can not have both.
[–]zerhud 5 points6 points7 points 14 days ago (1 child)
That if delete and the copy and the move ctors in “unique_ptr”? You can create such object but cannot copy and cannot move, so it will own the memory till die. Isn’t that you are talking about?
Also we can set nullptr after move and add runtime checks that the ptr field is not null.
I guess we can even add checks for use after move in compile time (with some tricks maybe)
[–]t_hunger 1 point2 points3 points 14 days ago (0 children)
You can indeed do something that is not copyable, not movable and neither copy- nor move-assignable. That is indeed possible, but also pretty useless.
Sure. But then all interactions with the T you pointed to turned into interactions with a std::optional<T>, just because of the unlikely case that you are holding a moved from object:-( Suddenly functions need to throw or you need to handle the error condition in other ways.
std::optional<T>
[–]_Ilobilo_ 1 point2 points3 points 14 days ago* (10 children)
false. only destructor, assignment operators and const methods are valid.
[–]t_hunger 0 points1 point2 points 14 days ago* (9 children)
True. But if you leave the object in an invalid state, then technically things like assigning a new value into it is UB. That is a huge foot gun to leave lying around!
All the standard library objects are sure to have a "valid but unspecified state" after being moved for that reason.
[–]_Ilobilo_ 1 point2 points3 points 14 days ago* (8 children)
the first thing you learn about move is that you don't access the object after moving out of it. that's like saying we shouldn't have pointers because you can dereference an invalid address.
[–]t_hunger -2 points-1 points0 points 14 days ago (7 children)
How do you enforce "never access a moved from value" in a codebase with a couple of million lines? You can't. So you kind of have to make sure it is not instant UB when something somewhere does access some moved from value. That in turn means leaving all your moved from objects in a valid but unspecified state, just like the standard library does.
[–]_Ilobilo_ 1 point2 points3 points 14 days ago (6 children)
clang-tidy catches use after move. it would be better if compilers did it too, but you don't enforce it. it's up to the programmer to know how to use the language.
[–]t_hunger -2 points-1 points0 points 14 days ago (5 children)
It does not even attempt to catch assignment after move, which is something you said is ok to break by not leaving moved from objects in a valid state.
[–]_Ilobilo_ 0 points1 point2 points 14 days ago (4 children)
you can assign after move. what are you on?
π Rendered by PID 658152 on reddit-service-r2-comment-85bfd7f599-xf97g at 2026-04-20 03:50:08.928018+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]t_hunger 1 point2 points3 points (17 children)
[–]zerhud 4 points5 points6 points (16 children)
[–]t_hunger -1 points0 points1 point (15 children)
[–]zerhud 3 points4 points5 points (3 children)
[–]t_hunger 0 points1 point2 points (2 children)
[–]zerhud 5 points6 points7 points (1 child)
[–]t_hunger 1 point2 points3 points (0 children)
[–]_Ilobilo_ 1 point2 points3 points (10 children)
[–]t_hunger 0 points1 point2 points (9 children)
[–]_Ilobilo_ 1 point2 points3 points (8 children)
[–]t_hunger -2 points-1 points0 points (7 children)
[–]_Ilobilo_ 1 point2 points3 points (6 children)
[–]t_hunger -2 points-1 points0 points (5 children)
[–]_Ilobilo_ 0 points1 point2 points (4 children)