C++标准模板(STL)- 类型支持 (类型特性,is_void,is_null_pointer,is_integral)

类型特性

类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。

试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。

定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。

类型属性

|---|---|
| 定义于头文件 <type_traits> ||

基础类型类别

继承自 std::integral_constant

成员常量

|--------------|------------------------------------------|
| value [静态] | 若 T 为整数类型则为 true ,否则为 false (公开静态成员常量) |

成员函数

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

成员类型

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

检查类型是否为 void

复制代码
std::is_void

|---------------------------------------|
| template< class T > struct is_void; |

检查 T 是否为 void 类型。若 T 是类型 void 、 const void 、 volatile void 或 const volatile void ,则提供等于 true 的成员常量 value。否则, value 等于 false 。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_void_v = is_void<T>::value; | | (C++17 起) |

可能的实现
复制代码
template< class T >
struct is_void : std::is_same<void, typename std::remove_cv<T>::type> {};

检查类型是否为 std::nullptr_t

复制代码
std::is_null_pointer

|-----------------------------------------------|---|-----------|
| template< class T > struct is_null_pointer; | | (C++11 起) |

检查 T 是否为 std::nullptr_t 类型。

T 为 std::nullptr_t 、 const std::nullptr_t 、 volatile std::nullptr_t 或 const volatile std::nullptr_t 类型,则提供等于 true 的成员常量 value

否则, value 等于 false 。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|----------------------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_null_pointer_v = is_null_pointer<T>::value; | | (C++17 起) |

可能的实现
复制代码
template< class T >
struct is_null_pointer : std::is_same<std::nullptr_t,
typename std::remove_cv<T>::type> {};
注意

对于 std::nullptr_t , std::is_pointer 为 false ,因为它不是内建指针类型。

检查类型是否为整型

复制代码
std::is_integral

|-------------------------------------------|---|-----------|
| template< class T > struct is_integral; | | (C++11 起) |

检查 T 是否为整数类型。若 T 为 bool 、 char 、 char8_t 、 char16_t 、 char32_t 、 wchar_t 、 short 、 int 、 long 、 long long 类型,或任何实现定义的扩展整数类型,包含任何有符号、无符号及 cv 限定的变体。则提供等于 true 的成员常量 value 。否则, value 等于 false 。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|--------------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_integral_v = is_integral<T>::value; | | (C++17 起) |

调用示例

复制代码
#include <iostream>
#include <type_traits>

class A {};

enum E : int {};

template <class T>
T f(T i)
{
    static_assert(std::is_integral<T>::value, "Integral required.");
    return i;
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_void<void>::value:    "
              << std::is_void<void>::value << std::endl;
    std::cout << "std::is_void<int>::value:     "
              << std::is_void<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_null_pointer<decltype(nullptr)>::value:   "
              << std::is_null_pointer<decltype(nullptr)>::value << std::endl;
    std::cout << "std::is_null_pointer< int* >::value:              "
              << std::is_null_pointer< int* >::value << std::endl;
    std::cout << "std::is_pointer< decltype(nullptr) >::value:      "
              << std::is_pointer< decltype(nullptr) >::value << std::endl;
    std::cout << "std::is_pointer<int*>::value:                     "
              << std::is_pointer<int*>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_integral<A>::value:       "
              << std::is_integral<A>::value << std::endl;
    std::cout << "std::is_integral<E>::value:       "
              << std::is_integral<E>::value << std::endl;
    std::cout << "std::is_integral<float>::value:   "
              << std::is_integral<float>::value << std::endl;
    std::cout << "std::is_integral<int>::value:     "
              << std::is_integral<int>::value << std::endl;
    std::cout << "std::is_integral<bool>::value:    "
              << std::is_integral<bool>::value << std::endl;
    std::cout << f(123) << std::endl;

    return 0;
}
输出
相关推荐
闻缺陷则喜何志丹19 分钟前
【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
数据结构·c++·前缀和·宽度优先·洛谷·并集查找
序属秋秋秋2 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
十秒耿直拆包选手9 小时前
Qt:主窗体(QMainwindow)初始化注意事项
c++·qt
霖0011 小时前
C++学习笔记三
运维·开发语言·c++·笔记·学习·fpga开发
mit6.82411 小时前
[shad-PS4] Vulkan渲染器 | 着色器_重新编译器 | SPIR-V 格式
c++·游戏引擎·ps4
tan77º12 小时前
【Linux网络编程】Socket - TCP
linux·网络·c++·tcp/ip
Mike_Zhang13 小时前
C++使用WinHTTP访问http/https服务
c++
CHANG_THE_WORLD13 小时前
「macOS 系统字体收集器 (C++17 实现)」
开发语言·c++·macos
GiraKoo13 小时前
【GiraKoo】Breakpad 崩溃分析系统
c++
妄想出头的工业炼药师14 小时前
python和C++相互调用使用
开发语言·c++