std::is_arithmetic
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_arithmetic; |
(C++11以降) | |
std::is_arithmetic は 単項型特性 (UnaryTypeTrait) です。
T が算術型(すなわち、整数型または浮動小数点型)であるか、その cv-qualified バージョンである場合、メンバー定数 value は true となります。それ以外の型の場合、value は false となります。
プログラムが std::is_arithmetic または std::is_arithmetic_v(C++17 以降) の特殊化を追加した場合、その動作は未定義です。
目次 |
[編集] テンプレートパラメータ
| T | - | チェックする型 |
[編集] ヘルパー変数テンプレート
| template< class T > constexpr bool is_arithmetic_v = is_arithmetic<T>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T が算術型の場合は true、それ以外の場合は false(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[編集] 備考
算術型とは、算術演算子 (+, -, *, /) が定義されている (おそらく通常の算術変換と組み合わせて) 組み込み型のことです。
すべての算術型に対して std::numeric_limits の特殊化が提供されます。
[編集] 可能な実装
template<class T> struct is_arithmetic : std::integral_constant<bool, std::is_integral<T>::value || std::is_floating_point<T>::value> {}; |
[編集] 例
このコードを実行
#include <atomic> #include <cstddef> #include <type_traits> class A {}; enum class B : int { e }; static_assert( std::is_arithmetic_v<bool> == true and std::is_arithmetic_v<char> == true and std::is_arithmetic_v<char const> == true and std::is_arithmetic_v<int> == true and std::is_arithmetic_v<int const> == true and std::is_arithmetic_v<float> == true and std::is_arithmetic_v<float const> == true and std::is_arithmetic_v<std::size_t> == true and std::is_arithmetic_v<char&> == false and std::is_arithmetic_v<char*> == false and std::is_arithmetic_v<int&> == false and std::is_arithmetic_v<int*> == false and std::is_arithmetic_v<float&> == false and std::is_arithmetic_v<float*> == false and std::is_arithmetic_v<A> == false and std::is_arithmetic_v<B> == false and std::is_arithmetic_v<decltype(B::e)> == false and std::is_arithmetic_v<std::byte> == false and std::is_arithmetic_v<std::atomic_int> == false ); int main() {}
[編集] 関連項目
| (C++11) |
型が整数型であるかをチェックする (クラステンプレート) |
| (C++11) |
型が浮動小数点型であるかをチェックする (クラステンプレート) |