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::unwrap_reference - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. type_traits
  3. unwrap_reference

最終更新日時: 2026年08月23日 14時23分10秒 (JST)
Akira Takahashi が更新

履歴 編集

class template
<type_traits>

std::unwrap_reference(C++20)

namespace std {
  template <class T>
  struct unwrap_reference {
    using type = …;
  };

  template <class T>
  using unwrap_reference_t = typename unwrap_reference<T>::type;
}

概要

reference_wrapper<T>型をT&型に展開する。

効果

  • テンプレートパラメータTがreference_wrapper<T>であれば、T&型をメンバ型typeとして定義する
  • そうでなければ、テンプレートパラメータTをメンバ型typeとして定義する

例

#include <cassert>
#include <functional>
#include <type_traits>

template <class T>
struct X {
  T t;
};

template <class T>
X<typename std::unwrap_reference<T>::type> f(T t)
{
  return X<typename std::unwrap_reference<T>::type>{t};
}

int main()
{
  static_assert(std::is_same_v<
    std::unwrap_reference<std::reference_wrapper<int>>::type,
    int&
  >);

  static_assert(std::is_same_v<
    std::unwrap_reference<int>::type,
    int
  >);

  static_assert(std::is_same_v<
    std::unwrap_reference<const int&>::type,
    const int&
  >);

  // Xクラスに参照を保持させたい場合にstd::ref()を通して渡し、
  // そうでなければ単に引数を転送する。
  int a = 3;
  X x = f(std::ref(a));
  x.t = 2;
  assert(a == 2);

  int b = 4;
  X y = f(b);
  y.t = 5;
  assert(y.t == 5);
  assert(b == 4);
}

出力

実装例

namespace std {
  template <class T>
  struct unwrap_reference { using type = T; };

  template <class T>
  struct unwrap_reference<reference_wrapper<T>> { using type = T&; };
}

バージョン

言語

  • C++20

処理系

  • Clang: 8.0 ✅
  • GCC: 9.1 ✅
  • Visual C++: ??

参照

  • P0318R1 unwrap_ref_decay and unwrap_reference
  • LWG Issue 3202. P0318R1 was supposed to be revised
    • C++20で、P0318R1の改訂漏れにより漏れていたunwrap_reference/unwrap_reference_t/unwrap_ref_decay/unwrap_ref_decay_tが<functional>シノプシスに追加された
  • LWG Issue 3348. __cpp_lib_unwrap_ref in wrong header
You are visitor 01402503 — 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.