这用 gcc 和 clang 编译
#include <type_traits>
struct A {
~A() = delete;
};
static_assert(std::is_trivially_copyable_v<A>);
int main() { }
删除析构函数的类是否可以简单地复制?
最佳答案
可简单复制的类可能没有删除的构造函数
鉴于该主题的标题和 future 的读者:该标准完全明确 A
定义为
struct A { ~A() = delete; };
根据 [class.prop]/1,不是可简单复制的类, 特别是/1.3
A trivially copyable class is a class:
- (1.1) that has at least one eligible copy constructor, move constructor, copy assignment operator, or move assignment operator ([special], [class.copy.ctor], [class.copy.assign]),
- (1.2) where each eligible copy constructor, move constructor, copy assignment operator, and move assignment operator is trivial, and
- (1.3) that has a trivial, non-deleted destructor ([class.dtor]).
至于为什么 GCC 和 Clang 都不尊重这一点,在 Clang 错误报告中
理查德·史密斯评论
(R. Smith) This is DR1734, which Clang (and apparently GCC) does not yet implement.
之后讨论继续讨论为什么这很难实现(ABI 兼容性问题),但没有提及实际尝试提交 DR 或类似内容以更改或挑战标准文本;例如。:
(R. Smith) [...] The part of the ABI I was referring to is "POD for the purpose of layout", for which various different targets use different rules, and the four that I listed above base their rule on Clang's broken notion of "trivially-copyable".
因此,从语言律师的角度来看,该主题的标准非常明确:
A
是 不是 可复制。
关于c++ - 删除析构函数的类被认为是可简单复制的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68841146/