名前空間
変種
操作

Move assignment operator

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定 (C++20未満)
noexcept 指定子 (C++11)
例外
名前空間
指定子
decltype (C++11)
auto (C++11)
alignas (C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr (C++11)
ユーザ定義 (C++11)
ユーティリティ
属性 (C++11)
typedef 宣言
型エイリアス宣言 (C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
コピー代入
ムーブ代入 (C++11)
デストラクタ
テンプレート
その他
 
クラスTのムーブ代入演算子は型operator=T&&const T&&、またはvolatile T&&のちょうど1つのパラメータを取り名前const volatile T&&と非鋳型非静的メンバ関数です。公共ムーブ代入演算子を使用した型はMoveAssignableです.
Original:
A move assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T&&, const T&&, volatile T&&, or const volatile T&&. A type with a public move assignment operator is MoveAssignable.
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 :: operator= ( class_name && ) (1) (C++11以上)
class_name & class_name :: operator= ( class_name && ) = default; (2) (C++11以上)
class_name & class_name :: operator= ( class_name && ) = delete; (3) (C++11以上)

[編集] 説明

ムーブ代入演算子の第典型宣言
Original:
# Typical declaration of a 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.
#ムーブ代入演算は、コンパイラによって生成されるように強制する
Original:
# Forcing a move assignment operator 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.
#暗黙のMOVEの割り当てを回避
Original:
# Avoiding implicit move assignment
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
それはオーバーロードの解決によって、例えば、選択されたときにムーブ代入演算子が呼び出されオブジェクトは右辺が同じ、または暗黙的に変換可能な型の右辺値で代入式の左側に表示されたときに.
Original:
The move assignment operator is called whenever it is selected by オーバーロードの解決, e.g. when an object appears on the left side of an assignment expression, where the right-hand side is an rvalue of the same or implicitly convertible type.
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 assignment operators 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, move-assigning from a std::string or from a std::vector leaves the right-hand side 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 assignment operators 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 move 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 destructors
    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 assignment operator 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とそのクラスのメンバーとして移動代入演算子を宣言します
Original:
then the compiler will declare a move assignment operator as an inline public member of its class with the signature
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラスは、複数の移動代入演算子などを持つことができますT& T::operator=(const T&&)T& T::operator=(T&&)両方。いくつかのユーザー定義のムーブ代入演算子が存在する場合でも、ユーザはキーワードdefaultと暗黙的に宣言されたムーブ代入演算子を強制的に生成させることができる.
Original:
A class can have multiple move assignment operators, e.g. both T& T::operator=(const T&&) and T& T::operator=(T&&). If some user-defined move assignment operators are present, the user may still force the generation of the implicitly declared move assignment operator 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.
いくつかの代入演算子(移動またはコピー)は、常にすべてのクラスで宣言されているため、基底クラスの代入演算子は常に隠されています。 using宣言は基本クラスから代入演算子をもたらすために使用され、その引数の型は、派生クラスの暗黙の代入演算子の引数の型と同じになることができれば、using宣言はまた、暗黙によって隠されている宣言.
Original:
Because some assignment operator (move or copy) is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
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 assignment operator 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.
  • Tconstである非静的データメンバを持っています
    Original:
    T has a non-static data member that is const
    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 of a reference type.
    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 that cannot be move-assigned (has deleted, inaccessible, or ambiguous 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 has direct or virtual base class that cannot be move-assigned (has deleted, inaccessible, or ambiguous 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 has a non-static data member or a direct or virtual base without a move assignment operator 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:
    T has a direct or indirect virtual base class
    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 assignment operator 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 assignment operator 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 assignment operator 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たかのようになりますされている些細なコピーassignmentoperatorと同じアクションを実行します。 C言語(POD型)と互換性のあるすべてのデータ型は自明移動アサイン可能です.
Original:
A trivial move assignment operator performs the same action as the trivial copy assignmentoperator, 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 move-assignable.
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)については、ムーブ代入演算子はスカラーとムーブ代入演算子のための作り付けの割り当てを使用して、その初期化の順序で、完全なオブジェクトの拠点のメンバーごとの移動代入と非静的メンバを実行クラス型の.
Original:
If the implicitly-declared move assignment operator 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 assignment operator copies the object representation (as by std::memmove). For non-union class types (class and struct), the move assignment operator performs full member-wise move assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and move assignment operator for class types.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] ノート

両方のコピーとムーブ代入演算子が用意されていれば、引数がある場合は、オーバーロードの解決は移動代入を選択'右辺値(いずれか' prvalueなど名無しの一時的またはとしてはxValueなどstd::moveの結果として、引数がある場合)、およびコピー代入を選択'左辺値(オブジェクトまたは関数/左辺値参照を返す演算子)と名付けました。のみコピー代入が提供されている場合は移動させた場合は、すべての引数のカテゴリは、コピー代入移動代入のためのフォールバックになりますれ、(限り、右辺値をconst参照にバインドすることができますので、それは、値またはconstへの参照として、その引数を取るように)それを選択利用できないです.
Original:
If both copy and move assignment operators are provided, overload resolution selects the move assignment 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 assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move 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:
The copy-and-swap 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& T::operator=(T arg) {
    swap(arg);
    return *this;
}

多くの場合、許容されるTの移動コンストラクタに1追加のコールのコストで右辺の引数に移動代入と同等の機能を実行します.
Original:
performs an equivalent of move assignment for rvalue arguments at the cost of one additional call to the move constructor of T, which is often acceptable.
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>
#include <utility>
 
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& operator=(const A&) { std::cout << "copy assigned\n"; return *this; }
    A& operator=(A&& other) {
         s = std::move(other.s);
         std::cout << "move assigned\n";
         return *this;
    }
};
 
A f(A a) { return a; }
 
struct B : A {
     std::string s2; 
     int n;
     // implicit move assignment operator B& B::operator=(B&&)
     // calls A's move assignment operator
     // calls s2's move assignment operator
     // and makes a bitwise copy of n
};
 
struct C : B {
    ~C() {}; // destructor prevents implicit move assignment
};
 
struct D : B {
    D() {}
    ~D() {}; // destructor would prevent implicit move assignment
    D& operator=(D&&) = default; // force a move assignment anyway 
};
 
int main()
{
    A a1, a2;
    std::cout << "Trying to move-assign A from rvalue temporary\n";
    a1 = f(A()); // move-assignment from rvalue temporary
    std::cout << "Trying to move-assign A from xvalue\n";
    a2 = std::move(a1); // move-assignment from xvalue
 
    std::cout << "Trying to move-assign B\n";
    B b1, b2;
    std::cout << "Before move, b1.s = \"" << b1.s << "\"\n";
    b2 = std::move(b1); // calls implicit move assignment
    std::cout << "After move, b1.s = \"" << b1.s << "\"\n";
 
    std::cout << "Trying to move-assign C\n";
    C c1, c2;
    c2 = std::move(c1); // calls the copy assignment operator
 
    std::cout << "Trying to move-assign E\n";
    D d1, d2;
    d2 = std::move(d1);
}

出力:

Trying to move-assign A from rvalue temporary
move assigned
Trying to move-assign A from xvalue
move assigned
Trying to move-assign B
Before move, b1.s = "test"
move assigned
After move, b1.s = "" 
Trying to move-assign C
copy assigned
Trying to move-assign E
move assigned