名前空間
変種
操作

Move constructors

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
クラスTのムーブコンストラクタは、すべてのデフォルト値を持っている最初のパラメータT&&const T&&volatile T&&、またはconst volatile T&&であり、どちらかの他のパラメータが存在しない、非テンプレートコンストラクタ、またはパラメータの残りです。公共ムーブコンストラクタを持つ型がMoveConstructibleです.
Original:
A move constructor of class T is a non-template constructor whose first parameter is T&&, const T&&, volatile T&&, or const volatile 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.

目次

[編集] 構文

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.
#移動コンストラクタがコンパイラによって生成されるように強制する
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.
#暗黙のムーブコンストラクタを回避
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.
オブジェクトが含まれ、同じタイプのは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.
  • bはタイプT a = std::move(b);ある初期化、またはT a(std::move(b));T
    Original:
    initialization, T a = std::move(b); or T a(std::move(b));, where b is of type T
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 関数の引数の受け渡し:f(std::move(a));aの型がどこTfvoid f(T t)です
    Original:
    function argument passing: f(std::move(a));, where a is of type T and f is void f(T t)
    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()がmoveコンストラクタを持つ型aあるTなどの関数内の.
    Original:
    function return: return a; inside a function such as T f(), where a is of type T 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.

[編集] 移動コンストラクタ暗黙的に宣言された

は、ユーザー定義のムーブコンストラクタはクラス型(structclass、または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.
  • は、ユーザーが宣言されていないコピーコンストラクタがあります
    Original:
    there are no user-declared copy constructors
    The 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 operators
    The 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 operators
    The 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 destructurs
    The 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 deleted
    The 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.
クラスには、例えば複数の移動コンストラクタを持つことができ、 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.

[編集] 暗黙的に宣言された移動コンストラクタを削除しました

クラス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.
  • 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 destructor
    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 a user-defined move constructor or move assignment operator
    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 is a union and has a variant member with non-trivial copy constructor
    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 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.
  • Tない仮想メンバ関数を持っていません
    Original:
    T has no virtual member functions
    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 no virtual base classes
    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 move constructor selected for every direct base of T is trivial
    The 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 of T is trivial
    The 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.

[編集] ムーブコンストラクタを暗黙的に定義された

暗黙的に宣言された移動コンストラクタが削除されたり、些細なことではないされている場合は、コンパイラによって(、関数本体が生成され、コンパイルされている)が定義されている。 unionタイプの場合、暗黙的に定義されたムーブコンストラクタは、オブジェクト表現を(std::memmoveするなど)がコピーされます。非組合クラス型に(classstruct)、ムーブコンストラクタは引数の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.

[編集] ノート

強い例外保証を可能にするために、ユーザー定義のムーブコンストラクタは例外をスローしてはいけません。実際には、標準コンテナは通常、コンテナ要素が再配置される必要があるとき移動とコピーの間で選択する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.
コピーと移動の両方のコンストラクタが用意されていれば、引数が右辺値'(どちら' 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.
多くの状況で、ムーブコンストラクタは、彼らが観察可能な副作用を引き起こす場合でも外に最適化されており、省略をコピーしてくださいを参照してください
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.

[編集]

#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 ctor
    std::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);
}

Output:

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