名前空間
変種
操作

Move assignment operator

提供: 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のムーブ代入演算子は型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);
}

Output:

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