Skip to content

Latest commit

 

History

History
82 lines (61 loc) · 2.01 KB

cref.md

File metadata and controls

82 lines (61 loc) · 2.01 KB

cref

  • functional[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp11[meta cpp]
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)
}
  • reference_wrapper[link reference_wrapper.md]

概要

変数への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;
}
  • std::cref[color ff0000]
  • std::reference_wrapper[link reference_wrapper.md]
  • r.get()[link reference_wrapper/get.md]

出力

4

バージョン

言語

  • C++11

処理系

参照