Ignoring the sanity of doing what I’m describing, does using the std::move() function improve construction time when used to pass an argument to a base constructor?
struct Bar {
Bar(std::string);
std::string _s;
}
struct Foo : public Bar {
Foo(std::string);
}
struct Bar(std::string bs) : _s(std::move(bs)) {
// 1
}
struct Foo(std::string fs) : Bar(std::move(fs)) {
// 2
}
So in this example, does the move() used in Foo‘s constructor prevent an additional copy of the string being made?
And for clarification, does this design mean no attempt should be made to use bs and fs at points // 1 and // 2, but using _s would be safe in both places?
To find out, I recoded your example with a fake
Stringclass like so:For me this prints out:
But if I remove this
std::movefrom theFooconstructor the printout changes to:So assuming a
String(String&&)is faster than aString(const String&), the former is faster. Otherwise, not.Correct.