c++ - 具有已删除复制构造函数的类是否可以轻松复制?

标签 c++ language-lawyer

是这个类吗:

class A {
  public:
    A() = default;
    A(const A&) = delete;
};

可以简单复制吗? (至少 clang 似乎是这么认为的(live))

尤其会

A a,b;
std::memcpy(&a, &b, sizeof(A));

调用未定义的行为?

上下文:This answer [因证明错误而被删除] 加上它的评论树。

最佳答案

更新:CWG 1734 的建议解决方案,当前处于“就绪”状态,会将 [class]/p6 修改为:

A trivially copyable class is a class:

  • where each copy constructor, move constructor, copy assignment operator, and move assignment operator (12.8 [class.copy], 13.5.3 [over.ass]) is either deleted or trivial,
  • that has at least one non-deleted copy constructor, move constructor, copy assignment operator, or move assignment operator, and
  • that has a trivial, non-deleted destructor (12.4 [class.dtor]).

这会呈现类似的类

struct B {
    B() = default;
    B(const B&) = delete;
    B& operator=(const B&) = delete;
};

不再是可复制的。 (这类类包括同步原语,如 std::atomic<T>std::mutex。)

但是,A在 OP 中有一个隐式声明的、非删除的复制赋值运算符,它是微不足道的,所以它仍然是微不足道的可复制的。

CWG1734 之前情况的原始答案保留在下面以供引用。


是的,有点违反直觉,它很容易复制。 [类]/p6:

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).

[class.copy]/p12:

A copy/move constructor for class X is trivial if it is not user-provided, its parameter-type-list is equivalent to the parameter-type-list of an implicit declaration, and if

  • class X has no virtual functions (10.3) and no virtual base classes (10.1), and
  • class X has no non-static data members of volatile-qualified type, and
  • the constructor selected to copy/move each direct base class subobject is trivial, and
  • for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;

类似地([class.copy]/p25):

A copy/move assignment operator for class X is trivial if it is not user-provided, its parameter-type-list is equivalent to the parameter-type-list of an implicit declaration, and if

  • class X has no virtual functions (10.3) and no virtual base classes (10.1), and
  • class X has no non-static data members of volatile-qualified type, and
  • the assignment operator selected to copy/move each direct base class subobject is trivial, and
  • for each non-static data member of X that is of class type (or array thereof), the assignment operator selected to copy/move that member is trivial;

[class.dtor]/p5:

A destructor is trivial if it is not user-provided and if:

  • the destructor is not virtual,
  • all of the direct base classes of its class have trivial destructors, and
  • for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

[dcl.fct.def.default]/p5:

A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration.

确实,这是 a source of problems for the committee itself , 因为在当前定义下 atomic<T> (以及互斥锁和条件变量)将是可轻松复制的。 (很明显,允许某人在 memcpyatomic 上使用 mutex 而不调用 UB 将是……让我们说严重的问题。)另见 N4460 .

关于c++ - 具有已删除复制构造函数的类是否可以轻松复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759441/

相关文章:

c++ - 空析构函数做什么?

c++ - vector<unique_ptr> 的初始化因复制错误而失败

c++ - pp-tokens 还剩下哪些字符?

c++ - 是否可以反序列化(从原始内存块)没有默认构造函数的对象?

c++ - 隐式生成的成员和 noexcept

c++ - 共享内存中的 SRW 锁

c++ - std::array 中的内存是否连续?

c++ - 您可以将 LPTSTR 转换为 BSTR 吗?

c - 如何让 gcc 对嵌入式系统和 void main(void) 感到满意

c++ - 匹配别名模板作为模板参数