哪些规则确定对象是否是可复制的 [英] Which rules determine whether an object is trivially copyable

查看:195
本文介绍了哪些规则确定对象是否是可复制的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着c ++ 11的引入,平凡的可复制性已经相当相关。最明显的是在使用'std :: atomic'。基本是很简单。如果满足以下条件,则类 foo 是可复制的:

  foo * src = new foo(); 
foo * dest = malloc(sizeof(foo));
memcpy(destination,src,sizeof(foo));

与以下效果相同:

  foo * src = new foo(); 
foo * dest = new foo(src);

因此,复制内存的对象将具有与复制构造函数相同的效果。但是,当然,是一个catch。不仅有复制构造函数。但也移动构造函数,移动赋值运算符。 Etc。



std :: is_trivially_copyable 可用于测试对象是否是可复制的。所以通过尝试和错误,可以使对象轻易地可复制。



但是当然一个明确的规则集会更好一点:)。所以我请求。

解决方案

最明确的规则集将直接来自标准。以下是标准草案N4296的相关条目:



可简单地在 [basic.types] / 9


Cv非限定标量类型,简单可复制类类型,这种类型的数组和非易失性
这些类型的const限定版本被统称为


三级可复制类在 [class] / 6


一个简单的可复制类是这样一个类:没有非平凡的复制构造函数,没有非平凡的移动构造函数,没有非平凡的复制赋值运算符,没有非平凡的移动赋值运算符,并且有一个简单的析构函数。


复制/移动 [class.copy] / 12中的构造函数


类X的复制/移动构造函数不是用户提供的,其参数类型列表等同于隐式声明的参数类型列表,并且如果类X没有虚拟函数并且没有虚拟基类,并且类X没有非静态数据成员并且选择用于复制/移动每个直接基类子对象的构造函数是微不足道的,并且对于类类型(或其数组)的X的每个非静态数据成员,构造函数选择
复制/移动该成员是微不足道的;


[class.copy] / 25中复制/移动赋值运算符


如果类X的复制/移动赋值运算符不是用户提供的, list等价于隐式声明的parameter-type-list,如果类X没有虚拟函数,没有虚拟基类,并且类X没有volatile限定类型的非静态数据成员,并且赋值运算符被选择复制/移动每个直接基类子对象是微不足道的,并且对于类型类型(或其数组)的X的每个非静态数据成员,被选择复制/移动该成员的赋值运算符
是微不足道的;


[class.dtor]中的析构函数/ 5


析构函数不是用户提供的,而且如果:析构函数不是虚函数,其类的直接基类具有简单析构函数,对于类类型(或其数组)的其类的所有非静态数据成员,每个这样的类具有简单析构函数。


简短的答案是,简短的答案有时比长答案更有帮助。 / p>

With the introduction of c++11, trivially copyableness has gotten quite relevant. Most notably in the use of 'std::atomic'. The basics are quite simple. A class foo is trivially copyable if:

foo* src = new foo();
foo* dest = malloc(sizeof(foo));
memcpy(dest, src, sizeof(foo));

Has the same effect as:

foo* src = new foo();
foo* dest = new foo(src);

So an object where copying the memory will have the same effect as a copy constructor. However there, of course, is a catch. There's not only copy constructors. But also move constructors, move assignment operators. Etc.

std::is_trivially_copyable can be used to test whether an object is trivially copyable. So with trial and error it is possible to make an object trivially copyable.

But of course a well defined set of rules would be a bit nicer:). So hereby my request.

解决方案

The most well-defined set of rules would come directly from the standard. Here are the relevant entries from standard draft N4296:

Trivially-copyable types are defined in [basic.types]/9

Cv-unqualified scalar types, trivially copyable class types, arrays of such types, and nonvolatile const-qualified versions of these types are collectively called trivially copyable types.

Trivially-copyable classes are defined in [class]/6

A trivially copyable class is a class that: has no non-trivial copy constructors, has no non-trivial move constructors, has no non-trivial copy assignment operators, has no non-trivial move assignment operators, and has a trivial destructor.

Copy/move constructors in [class.copy]/12

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 and no virtual base classes, 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; otherwise the copy/move constructor is non-trivial.

Copy/move assignment operators in [class.copy]/25

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 and no virtual base classes, 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; otherwise the copy/move assignment operator is non-trivial.

Destructors in [class.dtor]/5

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, 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. Otherwise, the destructor is non-trivial.

The short answer is that the short answer is sometimes more helpful than the long answer.

这篇关于哪些规则确定对象是否是可复制的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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