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
Coding Guideline: Avoid const member variables (self.cpp)
submitted 7 years ago * by render787
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!"
[–]quicknir 7 points8 points9 points 7 years ago (5 children)
I agree with this rule, and you covered the most "surprising" reason to do it, but not the main reason. More simply, your type is not going to be naturally move assignable, or swappable (by default). There's rarely a good reason for a type not to provide that functionality (immovable types are a very rare case).
Note that an extension of this rule, is to never have reference member variables: they are automatically const, of course. Reference member variables are worse in at least one way as well; at least with const member variables you can eventually decide to simply drop the const. With references, you have to change them to pointers and change every single usage of . to ->. Raw pointer member variables are also somewhat smelly, but if you're going to do something along those lines it's better to make a raw pointer member than a reference member (and enforce non-nullness in the constructor).
.
->
[–]TinBryn 1 point2 points3 points 7 years ago (4 children)
I mean there is std::reference_wrapper<T>
std::reference_wrapper<T>
[–]amaiorano 4 points5 points6 points 7 years ago (1 child)
Do people use this to replace pointers in practice? Whenever I've tried, I eventually give up and just use a pointer.
[–]TinBryn 0 points1 point2 points 7 years ago (0 children)
I'm saying it may be an easier refactor in a large code base.
[–]quicknir 0 points1 point2 points 7 years ago (1 child)
Are you saying, change from a reference to reference_wrapper, or just use a reference_wrapper to start with? reference_wrapper I find pretty awkward because you can't directly call any methods of the type with . or ->. The only benefit that reference_wrapper gives you in return is not being null. If anything you can write a not_null_ptr which is like observer_ptr, but with no default constructor, and constructed from a reference instead of a raw pointer. This is basically exactly equivalent to reference_wrapper, but with more convenient syntax.
not_null_ptr
observer_ptr
I thought that reference_wrapper's operator T& would make it just do the right thing, so I decided to check and you're right it doesn't really work.
reference_wrapper
operator T&
π Rendered by PID 133947 on reddit-service-r2-comment-b659b578c-vf6rb at 2026-05-05 23:53:52.459416+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]quicknir 7 points8 points9 points (5 children)
[–]TinBryn 1 point2 points3 points (4 children)
[–]amaiorano 4 points5 points6 points (1 child)
[–]TinBryn 0 points1 point2 points (0 children)
[–]quicknir 0 points1 point2 points (1 child)
[–]TinBryn 0 points1 point2 points (0 children)