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

履歴 編集

function
<functional>

std::reference_wrapper::operator=(C++11)

reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;           //C++11

constexpr reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept; //C++20

概要

参照先を切り替える

効果

*thisx.get()を指すように更新する

戻り値

*this

#include <iostream>
#include <functional>

int main()
{
  int x = 3;
  int y = 5;

  // xへの参照を保持する
  std::reference_wrapper<int> r(x);
  r = std::ref(y); // yへの参照を保持するよう切り替える

  r.get() += 1;

  std::cout << x << std::endl;
  std::cout << y << std::endl;
}

出力

3
6

バージョン

言語

  • C++11

処理系

参照