名前空間
変種
操作

Copy 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.
割り当てをコピーしてください
割り当てを移動します (C++11)
デストラクタです
テンプレート
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=TT&const T&、またはvolatile T&のちょうど1つのパラメータを取り名前const volatile T&と非鋳型非静的メンバ関数です。公共のコピー代入演算子を使用した型はCopyAssignableです.
Original:
A copy 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, T&, const T&, volatile T&, or const volatile T&. A type with a public copy assignment operator is CopyAssignable.
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= ( const class_name & ) (2) (C + + 11以来)
class_name & class_name :: operator= ( const class_name & ) = default; (3) (C + + 11以来)
class_name & class_name :: operator= ( const class_name & ) = delete; (4) (C + + 11以来)

[編集] 説明

copy-and-swap idiomが使用できるコピー代入演算子の第典型的な宣言
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom can be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
コピー·アンド·スワップイディオムを使用することはできませんコピー代入演算子の第典型的な宣言
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used
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 copy 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.
#暗黙のコピー代入を回避
Original:
# Avoiding implicit copy 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 copy assignment operator is called whenever selected by オーバーロードの解決, e.g. when an object appears on the left side of an assignment expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] コピー代入演算子を暗黙的に宣言された

は、ユーザー定義のコピー代入演算子がクラス型(structclass、またはunion)のために提供されていない場合、コンパイラは常にクラスのインラインpublicメンバとして1つを宣言します。次のすべてに該当する場合は、この暗黙的に宣言されたコピー代入演算子では、フォームT& T::operator=(const T&)があります
Original:
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T& T::operator=(const T&) 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.
  • Bそのパラメータであるコピー代入演算子を持ってTまたはBまたはconst B&。各直接基本const volatile B&
    Original:
    each direct base B of T has a copy assignment operator whose parameters are B or const B& or const volatile B&
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Mクラス型のクラス型または配列の各非静的データメンバTは、そのパラメータであるコピー代入演算子を持ってMまたはconst M&またはconst volatile M&
    Original:
    each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M& or const volatile M&
    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&)として宣言されています。 (これらの規則のために、暗黙的に宣言されたコピー代入演算子が揮発性左辺値引数にバインドできないことに注意してください)​​
Original:
Otherwise the implicitly-declared copy assignment operator is declared as T& T::operator=(T&). (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument)
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 copy assignment operators, e.g. both T& T::operator=(const T&) and T& T::operator=(T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy 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 the copy assignment operator 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 copy 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 copy-assigned (has deleted, inaccessible, or ambiguous copy 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 copy-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 user-declared move 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 user-declared 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:
The implicitly-declared copy 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 copy 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 copy 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であるかのようにオブジェクト表現のコピーを作成します。 C言語(POD型)と互換性のあるすべてのデータ型は自明コピーアサイン可能です.
Original:
A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-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 copy 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 copy assignment copies the object representation (as by std::memmove). For non-union class types (class and struct), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using, using built-in assignment for the scalars and copy 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.
暗黙的に定義されたコピー代入演算子の世代がある場合deprecated(C + + 11以来)Tユーザ宣言デストラクタまたはユーザ宣言コピーコンストラクタを持ってい.
Original:
The generation of the implicitly-defined copy assignment operator is deprecated(C + + 11以来) if T has a user-declared destructor or user-declared copy constructor.
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:
Copy assignment operator can be expressed in terms of copy constructor, destructor, and the swap() member function, if one is provided:
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) { // copy/move constructor is called to construct arg
    swap(arg);    // resources exchanged between *this and arg
    return *this;
}  // destructor is called to release the resources formerly held by *this

非投げスワップ()の場合、このフォームは強い例外保証を提供しています。右辺の引数を指定すると、このフォームが自動的に移動コンストラクタが呼び出され、時には "統一代入演算子"(のように、コピーと移動の両方)と呼ばれてい.
Original:
For non-throwing swap(), this form provides 強い例外保証. For rvalue arguments, this form automatically invokes the move constructor, and is sometimes referred to as "unifying assignment operator" (as in, both copy and move).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <iostream>
#include <memory>
struct A {
    int n;
    std::string s1;
    // user-defined copy assignment, copy-and-swap form
    A& operator=(A other) {
        std::cout << "copy assignment of A\n";
        std::swap(n, other.n);
        std::swap(s1, other.s1);
        return *this;
    }
};
 
struct B : A {
    std::string s2;
    // implicitly-defined copy assignment
};
 
struct C {
     std::unique_ptr<int[]> data;
     std::size_t size;
     // non-copy-and-swap assignment
     C& operator=(const C& other) {
         // check for self-assignment
         if(&other == this)
             return *this;
         // reuse storage when possible
         if(size != other.size)
             data.reset(new int[other.size]);
         std::copy(&other.data[0],
                   &other.data[0] + std::min(size, other.size),
                   &data[0]);
         return *this;
     }
     // note: copy-and-swap would always cause a reallocation
};
 
int main()
{
    A a1, a2;
    std::cout << "a1 = a2 calls ";
    a1 = a2; // user-defined copy assignment
 
    B b1, b2;
    b2.s1 = "foo";
    b2.s2 = "bar";
    std::cout << "b1 = b2 calls ";
    b1 = b2; // implicitly-defined copy assignment
    std::cout << "b1.s1 = " << b1.s1 << " b1.s2 = " << b1.s2 <<  '\n';
}

Output:

a1 = a2 calls copy assignment of A
b1 = b2 calls copy assignment of A
b1.s1 = foo b1.s2 = bar