GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::ref - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. functional
  3. ref

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function template
<functional>

std::ref(C++11)

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

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

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

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

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

概要

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

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

戻り値

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

例外

投げない

例

#include <iostream>
#include <functional>

int main()
{
  int x = 3;

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

  std::cout << r.get() << std::endl;
}

出力

4

バージョン

言語

  • C++11

処理系

  • Clang: 9.0.1 ✅
  • GCC: 4.7.0 ✅
  • Visual C++: ??

参照

  • P0357R3 reference_wrapper for incomplete types
    • テンプレートパラメータTに不完全型が許可された経緯
  • P1065R2 constexpr INVOKE
  • LWG Issue 688. reference_wrapper, cref unsafe, allow binding to rvalues
    • C++11で、const右辺値を受け取るオーバーロードがdelete定義され、一時オブジェクトへの参照を保持してしまう誤用がコンパイルエラーになるようになった
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.