std::is_floating_point
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_floating_point; |
(C++11 起) | |
std::is_floating_point
是一個 一元型別特性。
檢查 T
是否為浮點型別。提供成員常量 value,如果 T
是 float、double、long double,或任何擴充套件浮點型別(std::float16_t、std::float32_t、std::float64_t、std::float128_t 或 std::bfloat16_t)(C++23 起),包括任何 cv 限定變體,則該常量等於 true。否則,value 等於 false。
如果程式為 std::is_floating_point
或 std::is_floating_point_v
新增特化,則行為未定義。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool is_floating_point_v = is_floating_point<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
如果 T 是浮點型別(可能帶 cv 限定),則為 true,否則為 false(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
std::integral_constant<bool, value> |
[編輯] 可能的實現
template<class T> struct is_floating_point : std::integral_constant< bool, // Note: standard floating-point types std::is_same<float, typename std::remove_cv<T>::type>::value || std::is_same<double, typename std::remove_cv<T>::type>::value || std::is_same<long double, typename std::remove_cv<T>::type>::value // Note: extended floating-point types (C++23, if supported) || std::is_same<std::float16_t, typename std::remove_cv<T>::type>::value || std::is_same<std::float32_t, typename std::remove_cv<T>::type>::value || std::is_same<std::float64_t, typename std::remove_cv<T>::type>::value || std::is_same<std::float128_t, typename std::remove_cv<T>::type>::value || std::is_same<std::bfloat16_t, typename std::remove_cv<T>::type>::value > {}; |
[編輯] 示例
執行此程式碼
#include <type_traits> class A {}; static_assert(!std::is_floating_point_v<A>); static_assert(std::is_floating_point_v<float>); static_assert(!std::is_floating_point_v<float&>); static_assert(std::is_floating_point_v<double>); static_assert(!std::is_floating_point_v<double&>); static_assert(!std::is_floating_point_v<int>); int main() {}
[編輯] 參閱
[靜態] |
確定 IEC 559/IEEE 754 浮點型別 ( std::numeric_limits<T> 的公共靜態成員常量) |
(C++11) |
檢查型別是否為整型 (類模板) |
(C++11) |
檢查型別是否為算術型別 (類模板) |
(C++20) |
指定型別是浮點型別 (概念) |