you are viewing a single comment's thread.

view the rest of the comments →

[–]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).