Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguity in calling function. Implicit conversion?

#include <string>

void f(std::string&& rref){
}

void f(std::string s){ 
}

int main() {
    std::string s = "s";
    f(std::move(s));
}

This code causes an ambiguity and I don't know why, perhaps, I made explicit conversion to rvalue reference.

My idea is that rvalue reference can be implicitly converted to just lvalue. But I am not sure. Please explain.

like image 495
J. Doe Avatar asked Jan 08 '16 21:01

J. Doe


1 Answers

std::string can be initialized from an rvalue of type std::string . So the second function is a candidate.

It's not a feasible idea to have value and rvalue-reference overloads. A more normal setup is to have rvalue-reference, and lvalue-reference overloads:

void f(std::string&& rref);
void f(std::string & lref);   // or const&

This would cover all the use cases.

like image 68
M.M Avatar answered Sep 29 '22 12:09

M.M