是这个类吗:
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>
(以及互斥锁和条件变量)将是可轻松复制的。 (很明显,允许某人在 memcpy
或 atomic
上使用 mutex
而不调用 UB 将是……让我们说严重的问题。)另见 N4460 .
关于c++ - 具有已删除复制构造函数的类是否可以轻松复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759441/