- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在查找 lifetime of a temporary在 cppreference.com 上,我发现了一些与 C++14 不同的东西:
Whenever a reference is bound to a temporary or to a base subobject of a temporary, the lifetime of the temporary is extended to match the lifetime of the reference, with the following exceptions:
...
a temporary bound to a reference member in a constructor initializer list persists only until the constructor exits, not as long as the object exists. (note: such initialization is ill-formed as of DR 1696) (until C++14)
我查了一下标准,确实没有这样的说法。 ($12.2/5 临时对象 [class.temporary])
这是否意味着从 C++14 开始,临时绑定(bind)到引用成员的生命周期将延长到对象的生命周期?
我用 GCC 尝试了以下代码和 CLANG两者似乎都不是,临时将在构造函数结束时被销毁。
#include <iostream>
struct X {
~X() { std::cout << "X dtor\n"; }
};
struct Y {
Y() : x_(X()) { std::cout << "Y ctor\n"; }
const X& x_;
~Y() { std::cout << "Y dtor\n"; }
};
int main()
{
Y y;
std::cout << "Hello, world!\n";
}
结果:
Y ctor
X dtor
Hello, world!
Y dtor
我是不是理解错了?
最佳答案
您引用的缺陷报告已被采纳,N4582 已将新措辞包含在[class.base.init] 中:
A temporary expression bound to a reference member in a mem-initializer is ill-formed. [Example:
struct A {
A() : v(42) { } // error
const int& v;
};— end example ]
因此它不会延长对象的生命周期 - 代码只是格式错误。 gcc 和 clang 都会在我尝试过的每个版本上对您的代码发出警告,我认为这是符合要求的,但理想情况下它们应该在那里出错。
关于c++ - 绑定(bind)到成员初始值设定项列表中的引用成员的临时对象的生命周期 (C++14),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37549377/
我想成为 Spark 纱客户(link)。是否需要安装hadoop?还是只安装 yarn 可以吗? (by this link) 最佳答案 No Spark不需要Hadoop即可运行。 Apache
我是一名优秀的程序员,十分优秀!