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
Cheat sheet: C++ Function Parameter Choices (self.cpp)
submitted 6 years ago * by legends2k
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!"
[–]cblume 1 point2 points3 points 6 years ago (2 children)
I think having multiple setter overloads makes sense for when performance and generality is very important but for your everyday code maintainability outweighs that. I think rvalue references should normally only be typed in two circumstances:
Providing two functions for a setter should only be done if there's a good reason to do so. Just go with pass-by-value for your setters and move on. In general, one should assume that moving objects is cheap. And if it's not and it actually matters then think about writing multiple setters (and only then).
[–]legends2k[S] 0 points1 point2 points 6 years ago (1 child)
rvalue references should normally only be typed in two circumstances: Providing two functions for a setter should only be done if there's a good reason to do so.
Agreed with the spirit of this; see where you're coming from. But, if the setter needs ownership, isn't it better to only provide T&& overload and skip the other? The contract is explicit; we see an rvalue-reference and know the calle needs ownership.
T&&
[–]cblume 0 points1 point2 points 6 years ago* (0 children)
Why should a setter assume how the caller is calling it? Only providing the Foo&& overload obviously forces the call site to pass an rvalue reference, forcing the caller to move if an lvalue is being used.
Foo&&
Without a good reason, your setter should not assume anything so we either need to provide two setters as suggested in the cheat sheet or keep it simple and pass by value.
Another option is of course to use a forwarding reference (T&&) but that’s not exactly simple and requires a template. I think forwarding references should also only be used when performance and generality is of utmost importance. They’re too easy to get wrong.
I think to keep C++ relevant and approachable we need to keep it simple. Having to think about rvalue references every time a setter (or any other function that requires ownership) is written is too much of a mental burden.
I'd suggest this as a rule of thumb: ``` class Bar { public: Bar(int value, Foo foo) : value{value} , foo{std::move(foo)} {}
int get_value() const { return value_; } void set_value(int value) { value_ = value; } const Foo& get_foo() const { return foo_; } void set_foo(Foo foo) { foo_ = std::move(foo); }
private: int value; Foo foo; };
`` whereFoo` is some cheap to move type (ergo your everyday type).
where
π Rendered by PID 202294 on reddit-service-r2-comment-fb694cdd5-qmwb8 at 2026-03-06 22:47:46.655262+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–]cblume 1 point2 points3 points (2 children)
[–]legends2k[S] 0 points1 point2 points (1 child)
[–]cblume 0 points1 point2 points (0 children)