c++ type_index

在C++中,std::type_index<type_traits> 是两个非常强大的工具,用于处理类型信息和类型特性。以下是对它们的详细介绍,包括每个重要概念和函数的使用方法。


1. std::type_index

std::type_index 是一个轻量级的类型标识符,用于在运行时比较和存储类型信息。它通常与 std::type_info 一起使用,但提供了更安全的比较操作。

主要功能
  • 类型比较:可以比较两个类型是否相同。
  • 类型存储:可以将类型信息存储在容器中。
  • 类型名称获取:可以通过 name() 方法获取类型的名称。
构造函数
std::type_index(const std::type_info& ti);
  • 参数 ti 是一个 std::type_info 对象,通常通过 typeid 获取。
成员函数
  • name():返回类型的名称(与 std::type_info::name() 相同)。
  • hash_code():返回类型的哈希值,用于在哈希容器中存储类型信息。
示例代码
#include <iostream>
#include <typeindex>
#include <typeinfo>

int main() {
    std::type_index ti1(typeid(int));
    std::type_index ti2(typeid(double));
    std::type_index ti3(typeid(int));

    std::cout << "Type name of ti1: " << ti1.name() << std::endl;
    std::cout << "Type name of ti2: " << ti2.name() << std::endl;

    if (ti1 == ti3) {
        std::cout << "ti1 and ti3 are the same type." << std::endl;
    } else {
        std::cout << "ti1 and ti3 are different types." << std::endl;
    }

    return 0;
}
输出示例
Type name of ti1: i
Type name of ti2: d
ti1 and ti3 are the same type.

2. <type_traits>

<type_traits> 是一个头文件,提供了一组模板类和函数,用于在编译时检查类型的各种特性。这些特性包括类型是否是类、是否是整型、是否有默认构造函数等。

主要特性
  • 类型分类:判断类型是类、结构体、联合体、枚举等。
  • 类型属性:判断类型是否有默认构造函数、析构函数、拷贝构造函数等。
  • 类型转换:判断类型之间是否可以隐式转换。
  • 类型操作:如 std::remove_cvstd::add_pointer 等。
常见的类型特性
  • std::is_class<T>:判断类型 T 是否是类。
  • std::is_integral<T>:判断类型 T 是否是整型。
  • std::is_floating_point<T>:判断类型 T 是否是浮点型。
  • std::is_pointer<T>:判断类型 T 是否是指针。
  • std::is_reference<T>:判断类型 T 是否是引用。
  • std::is_const<T>:判断类型 T 是否是常量类型。
  • std::is_volatile<T>:判断类型 T 是否是易变类型。
  • std::is_default_constructible<T>:判断类型 T 是否有默认构造函数。
  • std::is_copy_constructible<T>:判断类型 T 是否有拷贝构造函数。
  • std::is_move_constructible<T>:判断类型 T 是否有移动构造函数。
  • std::is_trivially_copyable<T>:判断类型 T 是否是平凡可拷贝的。
类型操作
  • std::remove_cv<T>:移除类型 Tconstvolatile 修饰符。
  • std::add_pointer<T>:将类型 T 转换为指针类型。
  • std::remove_reference<T>:移除类型 T 的引用。
  • std::remove_extent<T>:移除数组类型 T 的维度。
  • std::remove_all_extents<T>:移除数组类型 T 的所有维度。
  • std::enable_if<Condition, T>:如果条件 Condition 为真,则提供类型 T
示例代码
#include <iostream>
#include <type_traits>

struct MyClass {};

int main() {
    std::cout << std::boolalpha; // 显示布尔值为 true/false

    std::cout << "Is MyClass a class? " << std::is_class<MyClass>::value << std::endl;
    std::cout << "Is int an integral type? " << std::is_integral<int>::value << std::endl;
    std::cout << "Is double a floating point type? " << std::is_floating_point<double>::value << std::endl;
    std::cout << "Is int* a pointer? " << std::is_pointer<int*>::value << std::endl;
    std::cout << "Is int& a reference? " << std::is_reference<int&>::value << std::endl;
    std::cout << "Is const int a const type? " << std::is_const<const int>::value << std::endl;

    // 类型操作
    std::cout << "Remove cv from const volatile int: " << typeid(std::remove_cv<const volatile int>::type).name() << std::endl;
    std::cout << "Add pointer to int: " << typeid(std::add_pointer<int>::type).name() << std::endl;

    return 0;
}
输出示例
Is MyClass a class? true
Is int an integral type? true
Is double a floating point type? true
Is int* a pointer? true
Is int& a reference? true
Is const int a const type? true
Remove cv from const volatile int: i
Add pointer to int: Pi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值