jeudi 30 mars 2017

Error occurred when passing r-value reference to r-value reference parameter [duplicate]

This question already has an answer here:

I have code

void print(string &&str) {
   cout << str << endl;
}
int main() {
   string tmp("Hello");
   string&& str = move(tmp);
   //print(move(str));
   print(str);
   return 0;
}

After compiling I get error: cannot bind rvalue reference of type 'std::__cxx11::string&&' to lvalue of type 'std::__cxx11::string'.

But str is r-value reference to r-value(isn't it?), so passing it into print makes sense I believe. Why this error occurred?

Aucun commentaire:

Enregistrer un commentaire