是一个具有删除的复制构造函数的类可以复制吗? [英] Is a class with deleted copy-constructor trivially copyable?

查看:130
本文介绍了是一个具有删除的复制构造函数的类可以复制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这个类:

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

可以复制吗? (至少clang似乎认为这样( live )))



特别是

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

调用未定义的行为?



此回答 [已删除,因为已验证的错误]及其注释树。

解决方案

更新 CWG 1734 < a>,当前处于ready状态,将修改[class] / p6为:


一个简单的可复制类是一个类:




  • 其中每个复制构造函数,移动构造函数,复制赋值运算符和移动赋值运算符(12.8 [class.copy],13.5.3
    [over.ass])是否已删除或不重要,

  • 至少有一个未删除的复制构造函数,移动构造函数,复制赋值运算符或移动赋值运算符$




这将呈现类

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

不再可复制。 (这种类包括诸如 std :: atomic< T> std :: mutex 的同步原语。 / p>

但是,OP中的 A 有一个隐式声明的非删除拷贝赋值操作符,



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






是的,有点违反直觉,它是可微不足道的可复制。 [class] / p6:


一个简单的可复制类是一个类:




  • 没有非平凡的复制构造函数(12.8),

  • 没有非平凡的移动构造函数(12.8),

  • 没有非平凡的副本分配运算符(13.5.3,12.8),

  • 没有非平凡的分配运算符(13.5.3,12.8)和

  • 有一个简单的析构函数(12.4)。




<复制] / p12:


类X的复制/移动构造函数如果不是
用户提供, parameter-type-list 等价于隐式声明的
参数类型列表,如果




  • 类X没有虚拟函数(10.3),没有虚拟基类(10.1),

  • 类X没有非静态数据成员和

  • 所选的复制/移动每个直接基类子对象的构造函数很简单,

  • 对每个非静态选择复制/移动该成员的构造函数为
    trivial;




同样([class.copy] / p25):


复制/如果不是用户提供的
,则X类的运算符是微不足道的,其 parameter-type-list 等价于
parameter-type-list 的隐式声明,如果




  • 类X没有虚拟函数(10.3),没有虚拟基类(10.1) / li>
  • 类X没有volatile限定类型的非静态数据成员,

  • 选择用于复制/移动每个直接基类子对象的赋值操作符对于类型(或其数组)的X的每个非静态数据成员,

  • ,选择复制/移动该成员的赋值运算符是
    琐碎;


[class.dtor] / p5:


析构函数不是用户提供的,而且如果:




  • 析构函数不是 virtual

  • 其类的所有直接基类都有简单的析构函数,

  • 对于类类型(或其数组)的类的所有非静态数据成员,每个这样的类都有一个简单的
    析构函数。


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


如果是用户声明的,并且没有在其第一个声明中显式
默认或删除,那么函数是用户提供的。


这确实是委员会本身的问题源,因为在当前定义原子 (以及互斥体和条件变量)简单可复制。 (显然,允许某人 memcpy 超过原子互斥体没有调用UB将是...让我们说严重的问题。)参见 N4460


Is this class:

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

trivially copyable? (At least clang seems to think so (live))

In particular, would

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

invoke undefined behavior?

Context: This answer [deleted because proven wrong] plus its comment tree.

解决方案

Update: The proposed resolution of CWG 1734, currently in "ready" status, would modify [class]/p6 to read:

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

This renders classes like

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

no longer trivially copyable. (Classes of this sort include synchronization primitives like std::atomic<T> and std::mutex.)

However, the A in the OP has a implicitly declared, non-deleted copy assignment operator that is trivial, so it remains trivially copyable.

The original answer for the pre-CWG1734 situation is preserved below for reference.


Yes, somewhat counterintuitively, it is trivially copyable. [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;

Similarly ([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.

Indeed, this has been 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.

这篇关于是一个具有删除的复制构造函数的类可以复制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆