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

履歴 編集

class template
<type_traits>

std::is_pointer(C++11)

namespace std {
  template <class T>
  struct is_pointer;

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

概要

Tがポインタ型かを調べる

効果

is_pointerは、型Tがポインタ型であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

備考

  • 関数へのポインタはポインタ型であるが、staticではないメンバへのポインタは、ポインタ型ではない。
  • nullptrはポインタ型へ変換できるが,nullptr_t型はポインタ型ではない。

#include <type_traits>
#include <cstddef>

static_assert(std::is_pointer<int*>::value == true, "value == true, int* is pointer");
static_assert(std::is_same<std::is_pointer<int*>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_pointer<int*>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_pointer<int*>() == true, "is_pointer<int*>() == true");

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

static_assert(std::is_pointer<unsigned*>::value == true, "unsigned* is pointer");
static_assert(std::is_pointer<const long*>::value == true, "const long* is pointer");
static_assert(std::is_pointer<const double* const>::value == true, "const double* const is pointer");
static_assert(std::is_pointer<void (*)()>::value == true, "void (*)() is pointer");

struct s
{
  void member_function(){};
  int member_object;
};

static_assert(std::is_pointer<int[]>::value == false, "int[] is not pointer");
static_assert(std::is_pointer<int*&>::value == false, "int*& is not pointer");
static_assert(std::is_pointer<void ()>::value == false, "void () is not pointer");
static_assert(std::is_pointer<void (s::*)()>::value == false, "void s::*() is not pointer");
static_assert(std::is_pointer<int s::*>::value == false, "int s::* is not pointer");
static_assert(std::is_pointer<std::nullptr_t>::value == false, "std::nullptr_t is not pointer");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.3.4, 4.5.3, 4.6.2, 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() を持っていないためエラーになる。また、GCC 4,3,4, 4.5.3 は nullptr_t 型に対応していないためエラーになる。

参照