技术

C++ type_traits总结

中文English
📌 一、引入版本📌 1. Introduced Versions
C++11 正式引入 <type_traits>,提供基础类型判断与变换模板。Introduced in C++11, providing basic type inspection and transformation templates.
C++14 添加 _v 简写形式(如 std::is_same_v<T, U>)。C++14 added variable template shortcuts (like std::is_same_v<T, U>).
C++17 添加逻辑组合模板(如 conjunction, disjunction)。C++17 added logical combination templates (e.g., conjunction, disjunction).
C++20 引入 Concepts,增强了类型约束能力。C++20 introduced Concepts to enhance type constraint support.

中文English
🎯 二、引入目的🎯 2. Purpose of Introduction
支持泛型编程,提高模板代码灵活性。Support generic programming with flexible templates.
编译期类型判断,增强类型安全性。Compile-time type checking for safety.
静态决策、条件启用、模板特化控制。Static decision-making, conditional enabling, and specialization control.
支持类型变换,如去除引用、指针、const 等。Support type transformations like removing references, pointers, and const.
提高性能(如选择性使用 memcpy)。Optimize performance (e.g., selectively use memcpy).

中文(类型判断模板)English (Type Inspection Templates)
std::is_same<T, U>:判断类型是否相同std::is_same<T, U>: Checks if two types are the same
std::is_integral<T>:是否为整型std::is_integral<T>: Checks for integral type
std::is_floating_point<T>:是否为浮点型std::is_floating_point<T>: Checks for floating-point type
std::is_pointer<T>:是否为指针类型std::is_pointer<T>: Checks for pointer type
std::is_array<T>:是否为数组类型std::is_array<T>: Checks for array type
std::is_class<T>:是否为类/结构体类型std::is_class<T>: Checks for class/struct type
std::is_enum<T>:是否为枚举类型std::is_enum<T>: Checks for enum type
std::is_base_of<Base, Derived>:基类关系std::is_base_of<Base, Derived>: Base/Derived check

中文(类型变换模板)English (Type Transformation Templates)
std::remove_const<T>:去除 conststd::remove_const<T>: Remove const
std::remove_reference<T>:去除引用std::remove_reference<T>: Remove reference
std::remove_pointer<T>:去除指针std::remove_pointer<T>: Remove pointer
std::add_const<T>:添加 conststd::add_const<T>: Add const
std::decay<T>:退化为普通类型std::decay<T>: Decay to regular type
std::common_type<T1, T2>:推导公共类型std::common_type<T1, T2>: Deduce common type

中文(条件控制模板)English (Conditional Control Templates)
std::enable_if<cond, T>:启用/禁用模板std::enable_if<cond, T>: Enable/disable templates
std::conditional<cond, T, U>:条件选择std::conditional<cond, T, U>: Conditional selection
std::conjunction<Ts…>:所有条件为真std::conjunction<Ts…>: All conditions must be true
std::disjunction<Ts…>:任一条件为真std::disjunction<Ts…>: At least one condition is true
std::negation<T>:取反std::negation<T>: Logical negation

💡 四、典型使用场景举例 | 💡 4. Typical Usage Examples

中文English
使用 if constexpr 进行类型分支Use if constexpr to branch by type
std::is_integral_v<T> 用于判断是否为整型Use std::is_integral_v<T> to check for integer type

template<typename T>

void print_type(T val) {

    if constexpr (std::is_integral_v<T>)

        std::cout << “整型: ” << val << ‘\n’;

    else if constexpr (std::is_floating_point_v<T>)

        std::cout << “浮点型: ” << val << ‘\n’;

    else

        std::cout << “其他类型\n”;

}


中文English
使用 std::enable_if 启用函数模板Use std::enable_if to enable templates

template<typename T>

typename std::enable_if<std::is_pointer<T>::value, void>::type

print_pointer_info(T ptr) {

    std::cout << “是指针\n”;

}


中文English
标签分发(Tag Dispatch)Tag Dispatch

template<typename T>

void process_impl(T, std::true_type) {

    std::cout << “是整型\n”;

}

template<typename T>

void process_impl(T, std::false_type) {

    std::cout << “不是整型\n”;

}

template<typename T>

void process(T val) {

    process_impl(val, std::is_integral<T>{});

}


中文English
优化 std::copy 内部行为Optimize behavior inside std::copy

template<typename T>

void copy_elements(const T* src, T* dest, size_t count) {

    if constexpr (std::is_trivially_copyable_v<T>)

        std::memcpy(dest, src, count * sizeof(T));

    else

        std::copy(src, src + count, dest);

}


🧾 总结 | 🧾 Summary

中文English
<type_traits> 是编译期类型工具箱,核心作用是提高模板代码的智能、灵活性与类型安全性,是现代 C++ 模板编程的重要基石。<type_traits> is a compile-time type utility toolbox. It empowers template code to be smarter, more flexible, and type-safe—an essential foundation of modern C++ metaprogramming.