std::is_arithmetic
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_arithmetic; |
(C++11 起) | |
std::is_arithmetic
是一個 一元型別特性。
如果 T
是算術型別(即,整型或浮點型)或其 cv-限定
版本,則提供成員常量 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 [靜態] |
如果 T 是算術型別,則為 true,否則為 false(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
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) |
檢查型別是否為浮點型別 (類模板) |