所以我一直在学习右值和右值引用,并且在试验时遇到了一些代码,我无法解决这些错误。
int&& test1(int& t)
{
return static_cast<int&&>(t);
}
std::string&& test2(std::string& t)
{
return static_cast<std::string&&>(t);
}
int main()
{
int n ;
std::string s;
static_cast<int&&>(n) = 9; //Error: expression must be a modifiable lvalue
static_cast<std::string&&>(s) = "test"; //Compiles and runs fine
test1(n) = 4; //Error: expression must be a modifiable lvalue
test2(s) = "hello"; //Compiles and runs fine
}
我只是想知道如何处理 std::strings 和 int 的右值引用有什么区别,以及为什么一个有效而一个无效。
我正在使用带有 C++17 的 Visual Studio 2019