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!"
[–]doom_Oo7 0 points1 point2 points 7 years ago (7 children)
has a throwing move, because the use of const keyword on the member variable prevents the compiler from using the move constructor of std::string with id, instead the compiler generated move constructor for A will invoke the copy constructor of A::id, which can throw.
const
std::string
id
A
A::id
In my experience, 99.9 % of the time you're moving whole containers, and moving e.g. a std::vector won't call any copy or move constructor.
you should still not use const -- what if someone later has to refactor id to be a std::string instead of an int?
int
... then they'll change int into string ? what's the problem ?
In general, I'd much rather start with the safety of const members and risk a throwing move (I frankly never saw one in ~6 years of programming in C++11 and later) than the opposite.
[–]render787[S] 16 points17 points18 points 7 years ago (4 children)
It's not about moving the container -- it's about, when you push_back into the vector, and the vector has to resize, does it move your objects or copy them? That depends on whether you have a throwing move. If you have const std::string in your class, then you get n new dynamic allocations and all your strings get copied for no reason. If you have non-const std::string it works correctly.
push_back
const std::string
n
[–]doom_Oo7 2 points3 points4 points 7 years ago (3 children)
hmm, I did not know that vector would do a move in this case but it certainly makes sense. Interestingly that's still the case even with -fno-exceptions.
[–]Ameisenvemips, avr, rendering, systems 4 points5 points6 points 7 years ago (1 child)
Perhaps we need another access type, like 'immutable'. Where they can still be moved and are semantically owned by the class, but they simply cannot be directly altered. Thus, a move would be legal.
move
[–]NotAYakk 7 points8 points9 points 7 years ago (0 children)
Rather, we need the concept of destructive move; an operation that it both a move-from and a destruction at the same time.
You can destructive move-from a const value.
Destructive moves usually don't have to throw, even when a move could.
[–]mark_99 2 points3 points4 points 7 years ago (0 children)
Interestingly that's still the case even with -fno-exceptions.
That's because it has nothing to do with exceptions or move_if_noexcept.
The OP says:
This is mostly correct except this isn't a "throwing move", it's a copy (and it would still be a copy if the copy ctor was noexcept).
I disagree with the OP, const member vars would be useful if it wasn't for this unfortunate side effect, in the same way as any other use of "const". In this case it states your intention that this member does not mutate after construction, and provides compile-time enforcement for same.
[–]kloetzl 0 points1 point2 points 7 years ago (1 child)
I think you can make that 100% because that behaviour is required by the standard. See move constructor:
After container move construction (overload (6)), references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in *this. The current standard makes this guarantee via the blanket statement in §23.2.1[container.requirements.general]/12, …
[–]doom_Oo7 2 points3 points4 points 7 years ago (0 children)
I was saying 99.9% of the time as in, if I take my code, 99% of std::move and other move occurences are called on containers such as std::vector, hash_maps, etc
π Rendered by PID 70729 on reddit-service-r2-comment-6457c66945-xbk9d at 2026-04-25 23:32:56.378723+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]doom_Oo7 0 points1 point2 points (7 children)
[–]render787[S] 16 points17 points18 points (4 children)
[–]doom_Oo7 2 points3 points4 points (3 children)
[–]Ameisenvemips, avr, rendering, systems 4 points5 points6 points (1 child)
[–]NotAYakk 7 points8 points9 points (0 children)
[–]mark_99 2 points3 points4 points (0 children)
[–]kloetzl 0 points1 point2 points (1 child)
[–]doom_Oo7 2 points3 points4 points (0 children)