c++ - 删除析构函数的类被认为是可简单复制的?

标签 c++ gcc clang language-lawyer

这用 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 错误报告中
  • Bug 39050 - is_trivially_copyable_v misbehaves

  • 理查德·史密斯评论

    (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/

    相关文章:

    C++ 重载指针的类型转换运算符

    c++ - 如何在 C++ 中查找和避免未初始化的原始成员?

    macos - 在 OS X Yosemite 上构建 GCC-4.9.2

    c++ - 链接 clang-llvm

    C++ Grand Parent 默认构造函数调用

    c++ - 如何在C++中设置从RAII样式智能对象池获取的对象?

    c++ - 在哪里列出了 gstreamer 总线消息类型?

    c++ - getter 方法的推荐签名是什么?

    c - 浮点异常的陷阱是如何生成的?

    c++ - g++ 无法覆盖退出函数