我知道有一些类似的主题已经(如
this).
这个主题的例子是:
std::string & rs1 = std::string();
显然,std :: string()是一个右值.但是,我的问题是为什么s1合法而s2不是?
const std::string& s1 = "String literal";
std::string& s2 = "String literal";
标准清楚地表明字符串文字是左值(这是可以理解的,因为它们在技术上是const char *).当我编译s2时,我得到以下内容:
prog.cpp:4:19: error: invalid initialization of non-const reference of type
'std::string& {aka std::basic_string&}' from an rvalue of type
'const char*' std::string& s2 = "String literal";
我明白,左值和右值的标准定义是互斥的,编译器这可能是一个错误吗?我在这个例子中使用gcc 4.9.2.这也是字面值真的是x值的情况之一吗?