Passing lvalue reference as an rvalue reference argument does not compile. Compiler could create a temporary object with its copy constructor and pass it as an rvalue, but it does not. Still, it does call the constructor, if types do not match.
I'm interested, why does it works that way? Which design logic is here in the C++ standard, that forces the compiler to treat copy-constructor in a different way?
void do_smth(string && s)
{}
void f(const char * s)
{
do_smth(s); // passes a temporary string constructed from const char*
}
void g(const string & s)
{
do_smth(s); // does not compile
}
void h(const string & s)
{
do_smth(string(s)); // ok again
}
What should I do if I do not want to implement the second signature do_smth(const string &)
? Should I use pass-by-value void do_smth(string s)
instead?
What are other differences betweeen by-value void do_smth(string s)
and rvalue-reference void do_smth(string && s)
, given the object string
has move constructor?