std::string 的右值和左值

时间:2021-05-14 03:11:39

标签: c++ g++ stdstring lvalue-to-rvalue

我运行了如图所示的代码,我对为什么 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

0 个答案:

没有答案