1

我运行如图所示的代码,我对为什么 a+b 是右值感到困惑。据我所知,右值应该只有一个数据或地址,在这种情况下 std::string 应该是一个左值,不是吗?


void foo(const std::string&& input){
    std::cout<<"RVALUE "<<input<<std::endl;
}
void foo(const std::string& input){
    std::cout<<"LVALUE "<<input<<std::endl;
}

int main(){
    std::string a = "Tom";
    std::string b = "Cruise"; 
    std::string c = a + b;
    foo(a+b);
    foo(c);
}

输出

 RVALUE TomCruise 
 LVALUE TomCruise
4

0 回答 0