最終更新日時(UTC):
が更新

履歴 編集

function
<utility>

std::pair::operator=

pair& operator=(const pair& p);                             // (1) C++03
constexpr pair& operator=(const pair& p);                   // (1) C++20

constexpr const pair& operator=(const pair& p) const;       // (2) C++23

template <class U, class V>
  pair& operator=(const pair<U, V>& p);                     // (3) C++03
template <class U, class V>
  constexpr pair& operator=(const pair<U, V>& p);           // (3) C++20

template<class U, class V>
constexpr const pair& operator=(const pair<U, V>& p) const; // (4) C++23

pair& operator=(pair&& p) noexcept(see below);              // (5) C++11
constexpr pair& operator=(pair&& p) noexcept(see below);    // (5) C++20

constexpr const pair& operator=(pair&& p) const;            // (6) C++23

template <class U, class V>
  pair& operator=(pair<U, V>&& p);                          // (7) C++11
template <class U, class V>
  constexpr pair& operator=(pair<U, V>&& p);                // (7) C++20

template<class U, class V>
  constexpr const pair& operator=(pair<U, V>&& p) const;    // (8) C++23

template<pair-like P>
  constexpr pair& operator=(P&& p);                         // (9) C++23
template<pair-like P>
  constexpr const pair& operator=(P&& p) const;             // (10) C++23

概要

  • (1) : 同じ型のpairをコピー代入する
  • (2) : (1) のプロキシ参照版
  • (3) : 変換可能なpairをコピー代入する
  • (4) : (3) のプロキシ参照版
  • (5) : 同じ型のpairをムーブ代入する
  • (6) : (5) のプロキシ参照版
  • (7) : 変換可能なpairをムーブ代入する
  • (8) : (6) のプロキシ参照版
  • (9) : pair-likeなオブジェクトを代入
  • (10) : (9) のプロキシ参照版

プロキシ参照版とは、プロキシ参照である(要素がどちらもプロキシ参照である)pairが持つ各要素について、その要素の参照先へ、他のpair又はpair-likeなオブジェクトの対応する値を代入する動作を行う版である。

要件

効果

  • (1), (2), (3), (4) : p.firstthis->firstに、p.secondthis->secondにコピー代入する
  • (5), (6), (7), (8) : p.firstthis->firstに、p.secondthis->secondにムーブ代入する
  • (9), (10) : get<0>(p)p.firstに、get<1>(p)p.secondに代入する

戻り値

*this

例外

#include <iostream>
#include <utility>
#include <string>

template <class T1, class T2>
void print(const std::string& name, const std::pair<T1, T2>& p)
{
  std::cout << name << " : (" << p.first << "," << p.second << ")" << std::endl;
}

int main()
{
  // コピー代入
  {
    std::pair<int, std::string> p(1, "abc");
    std::pair<int, std::string> p1;
    p1 = p;
    print("p1", p1);
  }

  // 変換可能なpairのコピー代入
  {
    std::pair<int, const char*> p(1, "abc");
    std::pair<int, std::string> p2;
    p2 = p;
    print("p2", p2);
  }

  // ムーブ代入
  {
    std::pair<int, std::string> p(1, "abc");
    std::pair<int, std::string> p3;
    p3 = std::move(p);
    print("p3", p3);
  }

  // 変換可能なpairのムーブ代入
  {
    std::pair<int, const char*> p(1, "abc");
    std::pair<int, std::string> p4;
    p4 = std::move(p);
    print("p4", p4);
  }
}

出力

p1 : (1,abc)
p2 : (1,abc)
p3 : (1,abc)
p4 : (1,abc)

バージョン

言語

  • C++11 : (5), (7)

処理系

  • Clang: ??
  • GCC: 4.6.1
  • ICC: ??
  • Visual C++: 2010, 2012, 2013, 2015
    • (1), (3)はそれより前から実装されている。

参照