为什么简单的可复制类需要析构函数必须是微不足道的 [英] Why trivial copyable class require the destructor must be trivial

查看:131
本文介绍了为什么简单的可复制类需要析构函数必须是微不足道的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于C ++标准。 普通可复制类定义如下:

Base on the C++ standard. The trivial copyable class are define as following:

根据9/5,

A trivially copyable class is a class that:
- has no non-trivial copy constructors (12.8),
- has no non-trivial move constructors (12.8),
- has no non-trivial copy assignment operators (13.5.3, 12.8),
- has no non-trivial move assignment operators (13.5.3, 12.8), and
- has a trivial destructor (12.4).

据我理解,平凡可复制类按位复制。
所以直觉和理由需要平凡析构函数,这与位元复制无关。

By my understanding, the trivially copyable class is which can be copied by bitwise copied. So what's the intuition and reason to require trivial destructor which is unrelated to the bitwise copy.

推荐答案

原因很简单。简单地可复制意味着它是有效和定义的行为,将一个对象A复制到另一个B的顶部。显然,B的析构函数不会被调用,因此它必须是微不足道的。

The reason is simple enough. Trivially copyable means that it is valid and defined behaviour to copy one object A over the top of another B. Obviously the destructor for B will not be called, so it has to be trivial.

在N3797 3.8 / 4有一个对象生命周期的描述,似乎覆盖了这种情况。然而,如果程序不依赖于析构函数的副作用,则可能存在关于重用存储和不调用非平凡析构函数的漏洞。

There is a description of object lifetime at N3797 3.8/4 which appears to cover the situation. However, there could be a loophole regarding reuse of storage and non-calling of non-trivial destructor provided the program does not depend on side effects of the destructor.

这篇关于为什么简单的可复制类需要析构函数必须是微不足道的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆