void foo(const std::string& n) { std::cout << "左值lvalue" << std::endl; } void foo(std::string&& n) { std::cout << "右值rvalue" << std::endl; } int main() { foo("hello"); std::string a = "world"; foo(a); foo(std::move(a)); //把左值按右值处理,并移交所有权 return 0; }
void foo(const std::string& n) { std::cout << "左值lvalue" << std::endl; } void foo(std::string&& n) { std::cout << "右值rvalue" << std::endl; } int main() { foo("hello"); std::string a = "world"; foo(a); foo(std::move(a)); //把左值按右值处理,并移交所有权 return 0; }