Should all/most setter functions in C++11 be written as function templates accepting universal references?

You know the classes A and B, so you know if they are movable or not and if this design is ultimately necessary. For something like std::string, it’s a waste of time changing the existing code unless you know you have a performance problem here. If you’re dealing with auto_ptr, then it’s time to rip it out and use unique_ptr.

It’s usually preferred now to take arguments by value if you don’t know anything more specific- such as

void set_a(A a) { _a = std::move(a); }

This permits the use of any of the constructors of A without requiring anything except movability and offers a relatively intuitive interface.

Leave a Comment