首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >生命周期扩展与条件算子

生命周期扩展与条件算子
EN

Stack Overflow用户
提问于 2013-01-19 03:06:01
回答 2查看 729关注 0票数 20

局部左值引用到常量引用和右值引用可以延长临时对象的生命周期:

代码语言:javascript
复制
const std::string& a = std::string("hello");
std::string&& b = std::string("world");

当初始化器不是一个简单的表达式,而是使用条件运算符时,这也有效吗?

代码语言:javascript
复制
std::string&& c = condition ? std::string("hello") : std::string("world");

如果其中一个结果是临时对象,而另一个不是,该怎么办?

代码语言:javascript
复制
std::string d = "hello";
const std::string& e = condition ? d : std::string("world");

当条件为假时,C++是否强制延长临时对象的生命周期?

这个问题是在回答this question关于不可复制对象的问题时出现的。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14405837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档