So I now a copy constructor can help us find rvalues in code. So should I use it here?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Example
{
Example(std::string&& example);
}
// does the copy constructor pick this up?
Example("Hello World!");
// what about this? or do I have to have another constructor for this?
std::string s = "Hi";
Example(s);
// or this?
Example e(std::string("Hey"));
No. Example still supplies both a copy and move constructor - they're provided implicitly by the compiler.
Example's implicit move constructor, whose signature is Example::Example(Example&&)
would be preferred over its implicit copy constructor, whose signature is Example::Example(Example const&)
when the argument expression is a non-const rvalue expression.