引言
在C++中,类型萃取(Type Traits)是一种在编译时获取和操作类型信息的机制。它通过模板元编程技术实现,允许开发者根据类型的特性进行条件编译或执行不同的代码路径。类型萃取类通常定义在
<type_traits>头文件中。
常见类型萃取类
std::is_* 系列
这些类型萃取类用于检查类型是否符合某种条件。
std::is_integral<T>:检查类型T是否为整数类型。std::is_floating_point<T>:检查类型T是否为浮点类型。std::is_pointer<T>:检查类型T是否为指针类型。std::is_reference<T>:检查类型T是否为引用类型。std::is_array<T>:检查类型T是否为数组类型。std::is_function<T>:检查类型T是否为函数类型。std::is_const<T>:检查类型T是否为常量类型。std::is_volatile<T>:检查类型T是否为易失类型。std::is_class<T>:检查类型T是否为类类型。std::is_union<T>:检查类型T是否为联合体类型。
std::is_*_v 系列(C++17引入)
这些是 std::is_* 系列的变量模板版本,简化了使用方式。
std::is_integral_v<T>:如果T是整数类型,返回true,否则返回false。std::is_floating_point_v<T>:如果T是浮点类型,返回true,否则返回false。
std::is_same<T, U>
std::is_same<T, U>:检查类型T和U是否相同。std::is_same_v<T, U>:这是std::is_same<T, U>的变量模板版本。
std::remove_* 系列
这些类型萃取类用于修改类型(如去除常量、指针等)。
std::remove_const<T>:去除类型T的常量修饰符。std::remove_volatile<T>:去除类型T的易失修饰符。std::remove_cv<T>:去除类型T的常量和易失修饰符。std::remove_reference<T>:去除类型T的引用。std::remove_pointer<T>:去除类型T的指针。std::remove_extent<T>:去除类型T的数组维度。
std::is_base_of<Base, Derived> 和 std::is_convertible<From, To>
std::is_base_of<Base, Derived>:检查Derived是否是Base的派生类。std::is_convertible<From, To>:检查类型From是否能转换为类型To。
std::enable_if<T, U> 和 std::disable_if<T, U>
std::enable_if<T, U>:当T为真时,启用类型U。std::disable_if<T, U>:当T为真时,禁用类型U。
std::is_trivially_* 系列(C++11引入)
这些类型萃取类用于检查类型是否具有某些简单的特性,如无构造函数、无拷贝构造函数等。
std::is_trivially_copyable<T>:检查类型T是否是一个 trivially copyable 类型(可以直接内存拷贝的类型)。std::is_trivially_constructible<T>:检查类型T是否可以通过简单的构造函数构造。std::is_trivially_destructible<T>:检查类型T是否有简单的析构函数。
std::conditional<T, U, V>(C++11引入)
这个类型萃取类根据条件 T 选择类型 U 或 V。
std::conditional<T, U, V>:如果T为真,则类型为U,否则为V。
is_integral<T>的实现
使用模板特化实现简单的类型萃取
|
|
基于c++14的变量的模板
|
|
解决修饰符问题
|
|
这样,就基本实现了自己的is_integral类了