std::is_fundamental

定义于头文件 <type_traits>
template< class T >
struct is_fundamental;
(C++11 起)

T 为基础类型(即算术类型、 voidnullptr_t ),则提供等于 true 的成员常量 value 。对于任何其他类型, valuefalse

目录

模板形参

T - 要检查的类型

辅助变量模板

template< class T >
inline constexpr bool is_fundamental_v = is_fundamental<T>::value;
(C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
T 为基础类型则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool ,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

可能的实现

template< class T >
struct is_fundamental
  : std::integral_constant<
        bool,
        std::is_arithmetic<T>::value ||
        std::is_void<T>::value  ||
        std::is_same<std::nullptr_t, typename std::remove_cv<T>::type>::value
> {};

示例

#include <iostream>
#include <type_traits>
 
class A {};
 
int main() 
{
    std::cout << std::boolalpha;
    std::cout << std::is_fundamental<A>::value << '\n';
    std::cout << std::is_fundamental<int>::value << '\n';
    std::cout << std::is_fundamental<int&>::value << '\n';
    std::cout << std::is_fundamental<int*>::value << '\n';
    std::cout << std::is_fundamental<float>::value << '\n';
    std::cout << std::is_fundamental<float&>::value << '\n';
    std::cout << std::is_fundamental<float*>::value << '\n';
}

输出:

false
true
false
false
true
false
false

参阅

(C++11)
检查是否是一个复合类型
(类模板)
检查类型是否为算术类型
(类模板)
(C++11)
检查类型是否为 void
(类模板)

版本历史

  • (当前 | 先前 2017年12月2日 (六) 08:16Fruderica讨论 | 贡献 . . (1,400字节) (-368). . (simplify) (撤销)
  • 当前 | 先前 2017年11月17日 (五) 08:50Fruderica讨论 | 贡献 . . (1,768字节) (-1). . (形参/辅助) (撤销)
  • 当前 | 先前 2017年7月25日 (二) 05:47Fruderica讨论 | 贡献. . (1,769字节) (-213). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:14P12bot讨论 | 贡献 . . (1,982字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:41P12bot讨论 | 贡献 . . (1,982字节) (-99). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:25P12bot讨论 | 贡献 . . (2,081字节) (+257). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 11:39P12讨论 | 贡献 . . (1,824字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,824字节) (-23). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:33P12讨论 | 贡献 . . (1,847字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,847字节) (+1,847). . (Translated from the English version using Google Translate)