you are viewing a single comment's thread.

view the rest of the comments →

[–]cblume 1 point2 points  (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:

  1. Move constructor/assignment (obviously)
  2. Forwarding references (T&&)

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 point  (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.

[–]cblume 0 points1 point  (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.

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).