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

履歴 編集

class template
<type_traits>

std::is_signed(C++11)

namespace std {
  template <class T>
  struct is_signed;

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

概要

Tが符号付き算術型か調べる

効果

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

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

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

#include <type_traits>

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

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

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

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

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
    • 2008は、TR1の定義に基づく実装となっている。すなわち、Tが整数型でなければ必ずfalse_typeになる。上記例ではstd::is_signed<float>が該当する。

備考

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

参照