局部左值引用到常量引用和右值引用可以延长临时对象的生命周期:
const std::string& a = std::string("hello");
std::string&& b = std::string("world");
当初始化器不是一个简单的表达式,而是使用条件运算符时,这也有效吗?
std::string&& c = condition ? std::string("hello") : std::string("world");
如果其中一个结果是临时对象,而另一个不是,该怎么办?
std::string d = "hello";
const std::string& e = condition ? d : std::string("world");
当条件为假时,C++是否强制延长临时对象的生命周期?
这个问题是在回答this question关于不可复制对象的问题时出现的。
https://stackoverflow.com/questions/14405837
复制相似问题