显式移动ctor是否消除隐式复制ctor? [英] Does an explicit move ctor eliminate implicit copy ctor?

查看:138
本文介绍了显式移动ctor是否消除隐式复制ctor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在已接受的答案中阅读此处


[a]不会为显式声明移动构造函数的类生成复制构造函数和复制赋值操作符


我注意到(g ++ 4.7.2),如果你定义一个移动构造函数,与 push_back()一起使用,如果你所做的一切都是 = delete 复制构造函数,得到一个隐式移动构造函数 - 你得到一个错误。 [...如果你没有明确地做任何事情,这让我想知道是哪个(移动或复制)实际使用...]



但是,此在线参考不同



因此,我的问题是,第一个引号是保证的,而不是在定义一个移动构造函数时隐式定义的。标准(包括)?对于一些需要显式析构函数的类,我宁愿用一个move构造函数和一个(删除)移动操作符来完成规则五,并且依赖于定义的隐式复制方法 。如果我不能依赖,那么我必须明确地 = delete 他们 - 但这是很多潜在的冗余的东西。

$ b $所以我的问题是,标准保证的第一个报价(包括


是的,您的第一个报价是由标准保证的。



标准报价(草案n3690):


12.8复制和移动类对象[class.copy] >



7 /如果类定义没有显式地声明一个拷贝构造函数,则会隐式声明。 如果类定义声明了move构造函数或move赋值运算符,则隐式声明的拷贝构造函数定义为deleted; 如果类具有用户声明的复制赋值运算符或用户声明的析构函数,则不推荐使用后者。



I read in the accepted answer here that:

[a] copy constructor and copy assignment operator won't be generated for a class that explicitly declares a move constructor or move assignment operator

I do notice (g++ 4.7.2) that if you define a move constructor, it will be used with, e.g., push_back(), whereas if all you do is = delete the copy constructor, you don't get an implicit move constructor -- you get an error. [...which leads me to wonder which one (move or copy) is actually used if you don't do anything explicitly...]

However, this online reference does not make the same explicit promises about the copy constructor not being implicitly defined when you define a move constructor.

So my question is, is the first quote guaranteed by the standard (including the "or")? I would prefer, with some classes which need an explicit destructor, to complete the "rule of five" with just a move constructor and a (deleted) move operator and rely on the implicit copy methods not being defined. If I can't rely on that, then I'll have to explicitly =delete them -- but that's a lot of potentially redundant stuff.

解决方案

So my question is, is the first quote guaranteed by the standard (including the "or")?

Yes, your first quote is guaranted by the standard.

Quote from the standard (draft n3690):

12.8 Copying and moving class objects [class.copy]

7/ If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

这篇关于显式移动ctor是否消除隐式复制ctor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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