名稱空間
變體
操作

std::is_arithmetic

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
is_arithmetic
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< class T >
struct is_arithmetic;
(C++11 起)

std::is_arithmetic 是一個 一元型別特性

如果 T 是算術型別(即,整型或浮點型)或其 cv-限定 版本,則提供成員常量 value 等於 true。對於任何其他型別,valuefalse

如果程式為 std::is_arithmeticstd::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() {}

[編輯] 參閱

檢查型別是否為整型
(類模板) [編輯]
檢查型別是否為浮點型別
(類模板) [編輯]