我一直认为,临时对象会一直存在到完整表达式的末尾。然而,std::vector
和数组的初始化之间有一个奇怪的区别。
请考虑以下代码:
#include <iostream>
#include <vector>
struct ID{
static int cnt;
// the number of living object of class ID at the moment of creation:
int id;
ID():id(++cnt){}
~ID(){
cnt--;
}
};
int ID::cnt=0;
int main(){
int arr[]{ID().id, ID().id};
std::vector<int> vec{ID().id, ID().id};
std::cout<<" Array: "<<arr[0]<<", "<<arr[1]<<"\n";
std::cout<<" Vector: "<<vec[0]<<", "<<vec[1]<<"\n";
}
这个程序的输出有点(至少对我来说)出乎意料:
Array: 1, 1
Vector: 1, 2
这意味着,临时对象在 std::vector
的整个初始化过程中是事件的,但在数组的情况下它们是一个接一个地创建和销毁的。我希望临时人员能够生存到完整表达式 int arr[]{ID().id, ID().id};
完成。
该标准提到了一个关于临时对象的生命周期和数组初始化的异常(exception) (12.2)。但是我不明白它的含义,也不知道为什么在这种特殊情况下应用它:
There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when a default constructor is called to initialize an element of an array. If the constructor has one or more default arguments, the destruction of every temporary created in a default argument is sequenced before the construction of the next array element, if any.
使用不同编译器的结果概述(MSVS 结果是 NathanOliver 的屈从):
Array Vector
clang 3.8 1, 2 1, 2
g++ 6.1 1, 1 1, 2
icpc 16 1, 1 1, 2
MSVS 2015 1, 1 1, 2
正如 ecatmur 所指出的,对于聚合初始化,braced-init-list 的每个元素都是一个完整表达式,因此以下代码
struct S{
int a;
int b;
} s{ID().id, ID().id};
std::cout<<" Struct: "<<s.a<<", "<<s.b<<"\n";
应该将 Struct 1, 1
打印到控制台。这正是 g++ 编译的程序所做的。但是,clang 似乎有一个错误 - 生成的程序打印 Struct 1, 2
。
已向 clang 报告了一个错误:https://llvm.org/bugs/show_bug.cgi?id=29080
最佳答案
这是core issue 1343 "Sequencing of non-class initialization" , 于 2016 年 11 月被论文 P0570R0 接受为缺陷报告.提议的决议是 C++17 的一部分,但因此不是 C++14 的一部分,因此(除非委员会决定发布 C++14 的更正)这是 C++17 和 C+ 之间的区别点+14。
C++14
根据C++14标准的规则,正确的输出是数组1, 1
和 vector 1, 2
;这是因为构造 vector (包括从braced-init-list)需要调用构造函数,而构造数组则不需要。
控制这一点的语言在 [intro.execution] 中:
10 - A full-expression is an expression that is not a subexpression of another expression. [...] If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition. [...]
这作为一个顶级概述很好,但它留下了一些未回答的问题:
- 准确地说,哪个语言构造算作产生函数隐式调用的构造;
- 实际上算作函数的隐式调用;大概对用户定义的构造函数的调用是对函数的调用,但是默认或定义为默认的构造函数呢?
数组是一个聚合,因此根据[dcl.init.aggr]从一个braced-init-list初始化;这表示每个元素直接从列表的对应元素初始化,因此没有隐式函数调用(至少不对应于整体初始化)。在语法级别,在 initializer ([dcl.init]/1) 中使用 braced-init-list 作为 大括号或相等初始化器,完整表达式是包含在大括号内并用逗号分隔的表达式。在每个完整表达式结束时,临时对象的析构函数都需要运行,因为 [class.temporary] 中提到的三个上下文都不是这里的情况。
vector 初始化的情况不同,因为您使用的是 initializer_list
构造函数,因此会发生函数的隐式调用(即 initializer_list
构造函数) ;这意味着在整个初始化过程中存在一个隐式的完整表达式,因此只有在 vector 的初始化完成时才会销毁临时对象。
令人困惑的是,[dcl.init.list] 说您的代码“大致相当于”:
const int __a[2] = {int{ID().id}, int{ID().id}}; // #1
std::vector<int> vec(std::initializer_list<int>(__a, __a + 2));
但是,这必须在上下文中读取 - 例如,支持 initializer_list
的数组的生命周期受 vector 初始化的限制。
这在 C++03 中要清楚得多,在 [intro.execution] 中有:
13 - [Note: certain contexts in C++ cause the evaluation of a full-expression that results from a syntactic construct other than expression (5.18). For example, in 8.5 one syntax for initializer is
( expression-list )
but the resulting construct is a function call upon a constructor function with expression-list as an argument list; such a function call is a full-expression. For example, in 8.5, another syntax for initializer is= initializer-clause
but again the resulting construct might be a function call upon a constructor function with one assignment-expression as an argument; again, the function call is a full-expression. ]
这一段是从 C++11 中完整删除的;这是根据 CWG 392 的决议.由此产生的困惑可能不是故意的。
C++17
在 P0570R0 之后,[intro.execution] 声明 full-expression 是:[...]
- an init-declarator ([dcl.decl]) [...] including the constituent expressions of the initializer, or [...]
- an expression that is not a subexpression of another expression and that is not otherwise part of a full-expression.
所以在 C++17 中,完整的表达式是 arr[]{ID().id, ID().id}
和 vec{ID().id, ID().id}
分别正确的输出是 1, 2
在每种情况下,因为第一个临时 ID
的销毁推迟到完整表达式的结尾。
关于c++ - 列表初始化期间临时对象的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025342/