For example, a class with two constructors, one taking a reference and the other taking a string r-value reference.
class A {
public:
A(std::string&& str) {
Foo foo(std::move(str));
//do something with foo. (e.g., use it to initialize some members)
field = foo.bar();
}
A(const std::string& str) {
Foo foo(str);
//do something with foo. (e.g., use it to initialize some members)
field = foo.bar();
}
};
If these two constructors perform virtually the same thing besides std::move appearing in the move constructor, is there any way to avoid having two nearly identical chunks of code?