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

履歴 編集

class template
<type_traits>

std::is_arithmetic(C++11)

namespace std {
  template <class T>
  struct is_arithmetic;

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

概要

型が算術型か調べる。算術型は、整数型、浮動小数点型、およびそれらのcv修飾を含む。

効果

is_arithmeticは、型Tが算術型であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

#include <type_traits>

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

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

static_assert(std::is_arithmetic<char>::value == true, "char is arithmetic");
static_assert(std::is_arithmetic<unsigned>::value == true, "unsigned is arithmetic");
static_assert(std::is_arithmetic<long long>::value == true, "long long is arithmetic");
static_assert(std::is_arithmetic<const float>::value == true, "const float is arithmetic");

enum e{};
static_assert(std::is_arithmetic<e>::value == false, "e is not arithmetic");
static_assert(std::is_arithmetic<int&>::value == false, "int& is not arithmetic");
static_assert(std::is_arithmetic<int ()>::value == false, "int () is not arithmetic");
static_assert(std::is_arithmetic<int[1]>::value == false, "int[1] is not arithmetic");

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

参照