为什么在评估完整表达式后调用临时对象的析构函数不是:
#include <iostream>
struct A
{
int a;
A();
~A();
};
A::~A()
{
std::cout << "~A()" << std::endl;
}
A::A()
{
std::cout << "A()" << std::endl;
}
int main()
{
A b = A(); //Constructing of temporary object and applies copy-initalization
std::cout << "side effect" << std::endl;
//Destructor calling.
}
Run Code Online (Sandbox Code Playgroud)
输出:
A()
side effect
~A()
Run Code Online (Sandbox Code Playgroud)
但是12.2/3 [class.temporary]说:
当实现引入具有非平凡构造函数(12.1,12.8)的类的临时对象时,它应确保为临时对象调用构造函数.类似地,应该使用非平凡的析构函数(12.4)调用析构函数.临时对象作为 评估全表达式(1.9)的最后一步被销毁,该表达式(词法上)包含创建它们的点.
| 归档时间: |
|
| 查看次数: |
280 次 |
| 最近记录: |