对于调用结果的复制初始化序列,它是否被认为是GCC和Clang的错误?
请看这个例子
#include <iostream>
struct A{
A(){
std::cout<<"A constructedn";
}
~A(){
std::cout<<"A destroyedn";
}
};
struct B{
B(int){
std::cout<<"B constructedn";
}
~B(){
std::cout<<"B destroyedn";
}
};
struct C{
C(){
std::cout<<"C constructedn";
}
~C(){
std::cout<<"C destroyedn";
}
bool operator==(B const&){
return true;
}
};
struct D{
D(bool){
std::cout<<"D constructedn";
}
};
bool fun(){
A a;
C c;
return c==0;
}
int main(){
D d = fun();
}
上面例子的输出在 GCC 和 Clang 中都是相同的,它们是
A constructed
C constructed
B constructed
B destroyed
C destroyed
A destroyed
D constructed
这意味着调用结果的复制初始化在该函数的局部或临时变量的所有销毁之后进行排序。它与以下规则相矛盾,即
[stmt.return#3]
调用的结果中的复制初始化之前测序在由return语句,这反过来,是局部变量的破坏之前测序的操作建立完整的表达年底的临时的破坏([语句.jump]) 包含 return 语句的块。
根据规则,输出应该是
A constructed
C constructed
B constructed
D constructed
B destroyed
C destroyed
A destroyed
是 GCC 和 Clang 的错误吗?
回答
相关函数的返回值是 a bool。这就是返回的内容。
您记录的输出没有显示bool返回值的复制初始化何时排序的证据,在其他任何事情之前或之后。
也恰好是一旦bool返回值,调用者就使用它来构造 的新实例D。但这bool与函数调用返回值的复制初始化语义完全无关。