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

履歴 編集

class template
<type_traits>

std::is_reference(C++11)

namespace std {
  template <class T>
  struct is_reference;

  template <class T>
  inline constexpr bool is_reference_v = is_reference<T>::value; // C++17
}

概要

Tが参照型か調べる

効果

is_referenceは、型Tが左辺値参照型もしくは右辺値参照型であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

#include <type_traits>

static_assert(std::is_reference<int&>::value == true, "value == true, int& is reference");
static_assert(std::is_same<std::is_reference<int&>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_reference<int&>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_reference<int&>() == true, "is_reference<int&>() == true");

static_assert(std::is_reference<int>::value == false, "value == false, int is not reference");
static_assert(std::is_same<std::is_reference<int>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_reference<int>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_reference<int>() == false, "is_reference<int>() == false");

static_assert(std::is_reference<float&>::value == true, "float& is reference");
static_assert(std::is_reference<unsigned&&>::value == true, "unsigned&& is reference");
static_assert(std::is_reference<long&&>::value == true, "long&& is reference");

static_assert(std::is_reference<int*>::value == false, "int* is not reference");
static_assert(std::is_reference<void (int&)>::value == false, "void (int&) is not reference");
static_assert(std::is_reference<int&& ()>::value == false, "int&& () is not reference");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.3.4, 4.5.3, 4.6.1, 4.7.0
  • Visual C++: 2008 (std::tr1), 2010, 2012, 2013, 2015

備考

上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 2010.0 は integral_constantoperator bool() を持っていないためエラーになる。

参照