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

履歴 編集

class template
<type_traits>

std::is_unsigned(C++11)

namespace std {
  template <class T>
  struct is_unsigned;

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

概要

Tが符号なし算術型か調べる

効果

is_unsignedは、型Tが符号なし算術型 (cv修飾を許容する) であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

符号なし算術型と見なす条件は以下:

  • C++11 : is_arithmetic<T>::value && T(0) < T(-1)
  • C++14 : is_arithmetic<T>::value == trueの場合、integral_constant<bool, T(0) < T(-1)>::valueの結果を真偽の結果とする。そうでなければ偽の結果とする。
    • 備考: Tが算術型以外だった場合に、T(0)T(-1)でテンプレートの置き換えが発生してしまうため、このような文言になっている。

#include <type_traits>

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

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

static_assert(std::is_unsigned<const volatile unsigned int>::value == true, "const volatile unsigned int is unsigned");
static_assert(std::is_unsigned<unsigned int&>::value == false, "unsigned int& is not unsigned");

class c{};
static_assert(std::is_unsigned<float>::value == false, "float is not unsigned");
static_assert(std::is_unsigned<c>::value == false, "class is not unsigned");

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 は integral_constantoperator bool() を持っていないためエラーになる。

参照