具有删除的复制构造函数的 class 是否可以平凡复制?

Is a class with deleted copy-constructor trivially copyable?

这是class:

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

可简单复制? (至少clang好像是这么认为的(live))

特别是会

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

调用未定义的行为?

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

更新CWG 1734 的提议决议,目前处于 "ready" 状态,将修改 [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]).

这呈现 class 就像

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

不再可以简单复制。 (类 这种类型包括像 std::atomic<T>std::mutex 这样的同步原语。)

然而,OP 中的 A 有一个隐式声明的、未删除的复制赋值运算符,它是平凡的,因此它仍然是平凡可复制的。

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


是的,有点违反直觉,它是可以简单复制的。 [class]/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, because under the current definition atomic<T> (along with mutexes and condition variables) would be trivially copyable. (And obviously, allowing someone to memcpy over an atomic or a mutex without invoking UB would be ... let's just say seriously problematic.) See also N4460