Move constructors
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
クラス
T
のムーブコンストラクタは、すべてのデフォルト値を持っている最初のパラメータT&&、const T&&、volatile T&&、またはconstvolatile T&&であり、どちらかの他のパラメータが存在しない、非テンプレートコンストラクタ、またはパラメータの残りです。公共ムーブコンストラクタを持つ型がMoveConstructible
です.Original:
A move constructor of class
T
is a non-template constructor whose first parameter is T&&, const T&&, volatile T&&, or constvolatile T&&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public move constructor is MoveConstructible
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集]構文
class_name ( class_name&& ) | (1) | (C++11およびそれ以降) | |||||||
class_name ( class_name&& ) = default; | (2) | (C++11およびそれ以降) | |||||||
class_name ( class_name&& ) = delete; | (3) | (C++11およびそれ以降) | |||||||
[編集]説明
ムーブコンストラクタの第典型宣言
Original:
# Typical declaration of a move constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#ムーブコンストラクタがコンパイラによって生成されるように強制する
Original:
# Forcing a move constructor to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#暗黙のムーブコンストラクタを回避
Original:
# Avoiding implicit move constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
オブジェクトが含まれ、同じタイプのはxValueから初期化されるたびに、ムーブコンストラクタが呼び出されます
Original:
The move constructor is called whenever an object is initialized from xvalue of the same type, which includes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 関数の引数の受け渡し:f(std::move(a));、
a
の型がどこT
とf
void f(T t)ですOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 関数の戻り値:return a;T f()がムーブコンストラクタを持つ型
a
あるT
などの関数内の.Original:function return: return a; inside a function such as T f(), wherea
is of typeT
which has a move constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
一般的に(動的に割り当てられたオブジェクトは、ファイル記述子、TCPソケットI / Oストリーム、実行中のスレッドなどに例えばポインタ)を引数に保持されているリソースを "盗む"のではなく、それらのコピーを作成し、引数に残しコンストラクタを移動いくつかの有効な、それ以外は不確定な状態。たとえば、std::stringから、またはstd::vectorから移動すると、引数が空になり.
Original:
Move constructors typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. For example, moving from a std::string or from a std::vector turns the argument empty.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]暗黙的に宣言されたムーブコンストラクタ
は、ユーザー定義のムーブコンストラクタはクラス型(struct、class、またはunion)、およびのために提供されていない場合は、次のすべてが当てはまる場合:
Original:
If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- は、ユーザーが宣言されていないコピーコンストラクタがありますOriginal:there are no user-declared copy constructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - は、ユーザーが宣言されたコピー代入演算子はありませんOriginal:there are no user-declared copy assignment operatorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - は、ユーザーが宣言されたムーブ代入演算子はありませんOriginal:there are no user-declared move assignment operatorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - ないユーザ宣言destructursはありませんOriginal:there are no user-declared destructursThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 削除されたように、暗黙的に宣言されたムーブコンストラクタが定義されないであろうOriginal:the implicitly-declared move constructor would not be defined as deletedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
コンパイラは署名
inline public
とそのクラスのT::T(T&&)
メンバーとしてムーブコンストラクタを宣言します Original:
then the compiler will declare a move constructor as an
inline public
member of its class with the signature T::T(T&&)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
クラスには、例えば複数のムーブコンストラクタを持つことができ、 T::T(const T&&)とT::T(T&&)両方。いくつかのユーザー定義のムーブコンストラクタが存在する場合でも、ユーザはキーワード
default
と暗黙的に宣言されたムーブコンストラクタの生成を強制するかもしれません.Original:
A class can have multiple move constructors, e.g. both T::T(const T&&) and T::T(T&&). If some user-defined move constructors are present, the user may still force the generation of the implicitly declared move constructor with the keyword
default
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]暗黙的に宣言されたムーブコンストラクタを削除しました
クラス
T
ための暗黙的に宣言された値またはデフォルトムーブコンストラクタは次のように定義されている'次のいずれかが真である削除されたOriginal:
The implicitly-declared or defaulted move constructor for class
T
is defined as deleted in any of the following is true:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
T
(削除された、アクセスできない、またはあいまいなムーブコンストラクタ)に移動することはできません非静的データメンバを持っていますOriginal:T
has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
(削除されたが、アクセスできない、またはあいまいなムーブコンストラクタ)に移動することはできません直接、仮想基底クラスを持っていますOriginal:T
has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
削除、またはアクセスできないデストラクタと直接、仮想基底クラスを持っていますOriginal:T
has direct or virtual base class with a deleted or inaccessible destructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
ユーザー定義のムーブコンストラクタまたはムーブ代入演算子を持っていますOriginal:T
has a user-defined move constructor or move assignment operatorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
組合であり、非自明なコピーコンストラクタを持つ異形部材を有するOriginal:T
is a union and has a variant member with non-trivial copy constructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
自明にコピーできませんムーブコンストラクタなしで非静的データメンバ、または直接または仮想基盤を持って.Original:T
has a non-static data member or a direct or virtual base without a move constructor that is not trivially copyable.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集]些細なムーブコンストラクタ
以下のすべての条件が真である場合、クラス
T
ための暗黙的に宣言されたムーブコンストラクタは些細です:Original:
The implicitly-declared move constructor for class
T
is trivial if all of the following is true:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
T
ない仮想メンバ関数を持っていませんOriginal:T
has no virtual member functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
ない仮想基底クラスを持っていませんOriginal:T
has no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
のすべての直接基本のために選択ムーブコンストラクタは些細ですOriginal:The move constructor selected for every direct base ofT
is trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
のmemeberすべての非静的クラス型(またはクラス型の配列)のために選択、ムーブコンストラクタは些細ですOriginal:The move constructor selected for every non-static class type (or array of class type) memeber ofT
is trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
些細なムーブコンストラクタは、オブジェクト表現のコピーstd::memmoveたかのようになりますされている些細なコピーコンストラクタと同じアクションを実行するコンストラクタです。 C言語(POD型)と互換性のあるすべてのデータ型は、些細なことに可動である.
Original:
A trivial move constructor is a constructor that performs the same action as the trivial copy constructor, that is, makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially movable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]ムーブコンストラクタを暗黙的に定義された
暗黙的に宣言されたムーブコンストラクタが削除されたり、些細なことではないされている場合は、コンパイラによって(、関数本体が生成され、コンパイルされている)が定義されている。 unionタイプの場合、暗黙的に定義されたムーブコンストラクタは、オブジェクト表現を(std::memmoveするなど)がコピーされます。非組合クラス型に(classとstruct)、ムーブコンストラクタは引数のxValueと直接初期化を使用して、その初期化の順序で、オブジェクトの塩基および非staticメンバの完全なメンバーワイズ移動を実行.
Original:
If the implicitly-declared move constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined move constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]ノート
強い例外保証を可能にするために、ユーザー定義のムーブコンストラクタは例外をスローしてはいけません。実際には、標準コンテナは通常、コンテナ要素が再配置される必要があるとき移動とコピーの間で選択するstd::move_if_noexceptに頼る.
Original:
To make strong exception guarantee possible, user-defined move constructors should not throw exceptions. In fact, standard containers typically rely on std::move_if_noexcept to choose between move and copy when container elements need to be relocated.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
コピーと移動の両方のコンストラクタが用意されていれば、引数が右辺値'(どちら' prvalueこのような一時的な名無しのかそのようなstd::moveの結果として、のxValueなど)である場合は、オーバーロードの解決は、ムーブコンストラクタを選択する、引数が左辺値'(オブジェクトまたは左辺値参照を返す関数/演算子の名前)である場合は、コピーコンストラクタを選択します。コピーコンストラクタが用意されている場合にのみ、すべての引数のカテゴリが移動するためのフォールバックのコピーになりますれ、(限り、右辺値をconst参照にバインドすることができますので、それは、constの参照を取るように)それを選択し、移動するときは使用できません.
Original:
If both copy and move constructors are provided, overload resolution selects the move constructor if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std::move), and selects the copy constructor if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy constructor is provided, all argument categories select it (as long as it takes reference to const, since rvalues can bind to const references), which makes copying the fallback for moving, when moving is unavailable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
多くの状況で、ムーブコンストラクタは、彼らが観察可能な副作用を引き起こす場合でも外に最適化されており、省略をコピーしてくださいを参照してください
Original:
In many situations, move constructors are optimized out even if they would produce observable side-effects, see 省略をコピーしてください
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]例
#include <string>#include <iostream> struct A {std::string s; A(): s("test"){} A(const A& o): s(o.s){std::cout<<"move failed!\n";} A(A&& o): s(std::move(o.s)){}}; A f(A a){return a;} struct B : A {std::string s2;int n;// implicit move-contructor B::(B&&)// calls A's move constructor// calls s2's move constructor// and makes a bitwise copy of n}; struct C : B { ~C(){};// destructor prevents implicit move}; struct D : B { D(){} ~D(){};// destructor would prevent implicit move D(D&&)=default;// force a move ctor anyway}; int main(){std::cout<<"Trying to move A\n"; A a1 = f(A());// move-construct from rvalue temporary A a2 =std::move(a1);// move-construct from xvalue std::cout<<"Trying to move B\n"; B b1;std::cout<<"Before move, b1.s = \""<< b1.s<<"\"\n"; B b2 =std::move(b1);// calls implicit move ctorstd::cout<<"After move, b1.s = \""<< b1.s<<"\"\n"; std::cout<<"Trying to move C\n"; C c1; C c2 =std::move(c1);// calls the copy constructor std::cout<<"Trying to move D\n"; D d1; D d2 =std::move(d1);}
出力:
Trying to move A Trying to move B Before move, b1.s = "test" After move, b1.s = "" Trying to move C move failed! Trying to move D