在C ++ 0x中的特殊成员函数 [英] Special member functions in C++0x

查看:197
本文介绍了在C ++ 0x中的特殊成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关特殊成员函数的维基百科文章不包含任何移动构造函数和移动的引用赋值运算符。

The Wikipedia article about special member functions doesn't contain any reference to move constructors and move assignment operators.

我想更新条目,但我不知道0x标准说什么。

I would like to update the entry but I'm not sure what the 0x standard says.

这两个函数的规则是什么?是否由编译器自动生成?

What are the rules regarding these two functions? Are they automatically generated by the compiler and if so when?

编辑:更新维基百科页面,如果任何人感觉到它,请帮助社区通过编辑它成形(如果需要)。

I've updated the Wikipedia page, if anyone feels like it please help the community by editing it into shape (if needed).

推荐答案

记住C ++ 0x不是很标准,这是可能改变。从 FCD (PDF链接) ,移动构造函数和移动赋值运算符确实可以显式默认,甚至隐式默认。 *

Keeping in mind C++0x isn't quite standard yet, this is subject to change. From the FCD (PDF link), move constructors and move assignment operators can indeed be explicitly defaulted, and even implicitly defaulted.*

我只是引用(大量缩略)一堆可能有用的东西:

I'm just going to quote (heavily abridged) a bunch of stuff that might be useful to glance at:

在显式默认的函数上,§8.4.2 / 1-2:

On explicitly-defaulted functions, §8.4.2/1-2:


显式默认的函数

A function that is explicitly defaulted shall


  • 是一个特殊的成员函数,

  • 具有与隐式声明相同的声明函数类型,

  • 默认参数,并且

  • 没有异常规范。

  • be a special member function,
  • have the same declared function type as if it had been implicitly declared,
  • not have default arguments, and
  • not have an exception-specification.

如果其第一个声明显式默认,

If it is explicitly defaulted on its first declaration,


  • 它应该是公开的,

  • 它不会是显式的,

  • 它不应是虚拟的,

  • 它被隐式地视为具有与隐式声明(15.4)相同的异常规范,并且在复制构造函数,move构造函数,复制赋值运算符或移动赋值运算符,则它的参数类型必须与隐式声明的参数类型相同。

  • it shall be public,
  • it shall not be explicit,
  • it shall not be virtual,
  • it is implicitly considered to have the same exception-specification as if it had been implicitly declared (15.4), and
  • in the case of a copy constructor, move constructor, copy assignment operator, or move assignment operator, it shall have the same parameter type as if it had been implicitly declared.

在特殊成员函数中,§12/ 1:

On special member functions, §12/1:


默认构造函数(12.8),移动构造函数和移动赋值运算符(12.8)和析构函数(12.4)是特殊成员函数。 [注:当程序没有显式地声明它们时,实现将隐式声明一些类类型的这些成员函数。如果使用它们,实现将隐式地定义它们。见12.1,12.4
和12.8。 -end note]

The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions. [ Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are used. See 12.1, 12.4 and 12.8. —end note ]

关于隐式声明的函数,§12.8/ 8-11:

About implicitly declared functions, §12.8/8-11:


如果类定义没有显式声明一个复制构造函数,并且没有用户声明的move构造函数,那么一个复制构造函数被隐式声明为default(8.4)。

If the class definition does not explicitly declare a copy constructor and there is no user-declared move constructor, a copy constructor is implicitly declared as defaulted (8.4).

类X的隐式声明的复制构造函数的形式为 X :: X(const X&) if

The implicitly-declared copy constructor for a class X will have the form X::X(const X&) if


  • X的每个直接或虚拟基类B都有一个复制构造函数,其第一个参数的类型为 const对于X的所有非静态数据成员,分别为B& const volatile B&

  • 它们是类类型M(或其数组),每个这样的类类型具有第一个参数是 const M& const volatile M&

  • each direct or virtual base class B of X has a copy constructor whose first parameter is of type const B& or const volatile B&, and
  • for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy constructor whose first parameter is of type const M& or const volatile M&.

否则,隐含声明的复制构造函数将具有 X :: X(X& / code>。

Otherwise, the implicitly-declared copy constructor will have the form X::X(X&).

如果类定义没有明确声明一个move构造函数,那么当且仅当

If the class definition does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if


  • X没有用户声明的复制构造函数,

  • move构造函数不会被隐式定义为已删除。

[注意:当移动构造函数未被隐式声明或显式提供时,否则已经调用move构造函数的表达式可以调用复制构造函数。 -end note]

[ Note: When the move constructor is not implicitly declared or explicitly supplied, expressions that otherwise would have invoked the move constructor may instead invoke a copy constructor. —end note ]

类X的隐式声明的移动构造函数将具有 X :: X(X&& code>。

The implicitly-declared move constructor for class X will have the form X::X(X&&).

在隐式删除的默认函数中,§12.8/ 12:

On implicitly deleted default functions, §12.8/12:


隐式声明的复制/移动构造函数是其类的内联公共成员。如果X具有:

An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy-/move constructor for a class X is defined as deleted (8.4.3) if X has:


  • 类X的默认复制/移动构造函数定义为已删除一个非平凡的相应构造函数,X是一个类似union的类,

  • 类型为M(或其数组)的非静态数据成员不能被复制/ (13.3)应用于M的相应构造函数,导致模糊性或函数从默认构造函数中删除或无法访问,或

  • 直接或虚拟基类B,不能复制/移动,因为应用于B的相应构造函数的重载分辨率(13.3)会导致模糊度或函数从默认构造函数中删除或无法访问,或

  • ,非静态数据成员或直接或虚拟基类,其类型没有移动构造函数,并且不可复制。

  • a variant member with a non-trivial corresponding constructor and X is a union-like class,
  • a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor, or
  • a direct or virtual base class B that cannot be copied/moved because overload resolution (13.3), as applied to B’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor, or
  • for the move constructor, a non-static data member or direct or virtual base class with a type that does not have a move constructor and is not trivially copyable.

§12.8/ 13-18定义了函数在隐式生成时如何工作。

§12.8/13-18 defines how the functions should work when they are implicitly generated.

§12.8/ 19与§12.8/ 8相同,除了复制赋值和移动赋值运算符。

§12.8/19 then does the same thing as §12.8/8 did, except with the copy-assignment and move-assignment operators. They are similar enough not to warrant quoting here.

为了更完整的图片,你需要阅读这些部分,但这是一般的想法。我很高兴我们得到隐式移动语义。

For a more complete picture, you'll want to read those sections in their entirety, but that's the general idea. I'm glad we get implicit move semantics.

*但是像默认的复制函数,正确的行为!大三应该成为大五。 (例如,当我们需要深度复制时,我们需要实现三巨头,我们还需要确保我们做一个深度移动,其中源的数据是空的/复位的,这是 done implicitly。)

*But like defaulted copy-functions, they might not always have the correct behavior! The Big Three should become The Big Five. (For example, The Big Three are implemented whenever we need to deep-copy something. We also need to make sure we do a "deep-move", where the source's data is nulled/reset. This is not done implicitly.)

这篇关于在C ++ 0x中的特殊成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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