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

履歴 編集

function template
<functional>

std::cref(C++11)

namespace std {
  template <class T>
  reference_wrapper<const T> cref(const T& t) noexcept;             // (1) C++11

  template <class T>
  constexpr reference_wrapper<const T> cref(const T& t) noexcept;   // (1) C++20

  template <class T>
  reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept; // (2) C++11

  template <class T>
  constexpr reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept; // (2) C++20

  template <class T>
  void cref(const T&&) = delete;                                    // (3)
}

概要

変数へのconst参照tを保持するreference_wrapperオブジェクトを生成する。

C++20からは、テンプレートパラメーターTは不完全型をサポートしている。

戻り値

  • (1) : tを参照するreference_wrapper<const T>オブジェクトを返す。
  • (2) : tをそのまま返す。

例外

投げない

#include <iostream>
#include <functional>

int main()
{
  int x = 3;

  // 参照ラッパーrは、変数xへのconst参照を保持する
  std::reference_wrapper<const int> r = std::cref(x);

  ++x;

  const int& rx = r.get();
  std::cout << rx << std::endl;
}

出力

4

バージョン

言語

  • C++11

処理系

参照