c++ typeid和type_index

2024-04-02 12:32
文章标签 c++ type index typeid

本文主要是介绍c++ typeid和type_index,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

typeid

查询类型的信息。
用于必须知晓多态对象的动态类型的场合以及静态类型鉴别。

使用方法

头文件: #include<typeinfo>
typeid(类型) 或者typeid(表达式)

  1. 指代一个表示 类型 的 std::type_info 对象。若 类型 为引用类型,则结果所指代的 std::type_info 对象表示被引用的类型。
  2. 检验表达式
    a) 若 表达式 为标识某个多态类型(即声明或继承至少一个虚函数的类)对象的泛左值表达式,则 typeid 表达式对该表达式求值,然后指代表示该表达式动态类型的 std::type_info 对象。若该泛左值表达式为通过对一个指针应用一元 * 运算符所得,且该指针为空指针值,则抛出 std::bad_typeid 类型或从 std::bad_typeid 派生的类型的异常。
    b) 若 表达式 不是多态类型的泛左值表达式,则 typeid 不对该表达式求值,而它所指代的 std::type_info 对象表示该表达式的静态类型。不进行左值到右值、数组到指针或函数到指针转换。然而对于纯右值参数,(形式上)要进行临时量实质化:typeid 确定其结果对象的类型。 (C++17 起)
    在所有情况下,typeid 都忽略 cv 限定符(即 typeid(T) == typeid(const T))

注意

应用于多态类型的表达式时,typeid 表达式的求值可能涉及运行时开销(虚表查找),其他情况下 typeid 表达式都在编译时解决。

typeid 所指代的对象的析构函数是否在程序结束时执行是未指明的。
不保证同一类型上的 typeid 表达式的所有求值都指代同一个 std::type_info 实例,不过这些 type_info 对象的 std::type_info::hash_code 相同,其 std::type_index 也相同。

const std::type_info& ti1 = typeid(A);
const std::type_info& ti2 = typeid(A);assert(&ti1 == &ti2); // 不保证
assert(ti1.hash_code() == ti2.hash_code()); // 保证
assert(std::type_index(ti1) == std::type_index(ti2)); // 保证

示例代码

#include <iostream>
#include <string>
#include <typeinfo>struct Base {}; // 非多态
struct Derived : Base {};struct Base2 { virtual void foo() {} }; // 多态
struct Derived2 : Base2 {};int main() {int myint = 50;std::string mystr = "string";double *mydoubleptr = nullptr;std::cout << "myint has type: " << typeid(myint).name() << '\n'<< "mystr has type: " << typeid(mystr).name() << '\n'<< "mydoubleptr has type: " << typeid(mydoubleptr).name() << '\n';// std::cout << myint 为多态类型的泛左值表达式;求值const std::type_info& r1 = typeid(std::cout << myint);std::cout << '\n' << "std::cout<<myint has type : " << r1.name() << '\n';// std::printf() 不是多态类型的泛左值表达式;不求值const std::type_info& r2 = typeid(std::printf("%d\n", myint));std::cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << '\n';// 非多态左值时为静态类型Derived d1;Base& b1 = d1;std::cout << "reference to non-polymorphic base: " << typeid(b1).name() << '\n';Derived2 d2;Base2& b2 = d2;std::cout << "reference to polymorphic base: " << typeid(b2).name() << '\n';try {// 解引用空指针:对于非多态表达式 OKstd::cout << "mydoubleptr points to " << typeid(*mydoubleptr).name() << '\n'; // 解引用空指针:对多态左值则不行Derived2* bad_ptr = nullptr;std::cout << "bad_ptr points to... ";std::cout << typeid(*bad_ptr).name() << '\n';} catch (const std::bad_typeid& e) {std::cout << " caught " << e.what() << '\n';}
}

输出:

myint has type: int
mystr has type: std::basic_string<char, std::char_traits<char>, std::allocator<char> >
mydoubleptr has type: double*
50
std::cout<<myint has type : std::basic_ostream<char, std::char_traits<char> >
printf("%d\n",myint) has type : int
reference to non-polymorphic base: Base
reference to polymorphic base: Derived2
mydoubleptr points to double
bad_ptr points to...  caught std::bad_typeid

样例2 lambda函数type_index

#include <iostream>
#include <functional>
#include <typeinfo>
#include <typeindex>
using namespace std;
class Base {};
class Derived: public Base {};void make_number(int a, int b) {std::cout << "a:" << a << " b:" << b << std::endl;
}int main() {std::cout << "class Base:" << (*(&typeid(Base))).name() << std::endl;std::cout << "class Derived:" << (*(&typeid(Derived))).name() << std::endl;// pointer 类型 typeinfostd::cout << "&make_number:" << typeid(&make_number).name() << std::endl;// 普通类型 typeinfostd::cout << "make_number:" << typeid(make_number).name() << std::endl;const std::type_info* make_number_info = &typeid(&make_number);typedef void (*func)(int, int);auto & tid = typeid(func); // auto  tid = typeid(func); // 报错,calling a provate constructstd::cout << "func :" << typeid(func).name() << std::endl;bool number = std::type_index(*(make_number_info)) == std::type_index(tid);std::cout << "bool type_index :" << number << std::endl;}

可能的输出:

class Base:4Base
class Derived:7Derived
&make_number:PFviiE
make_number:FviiE
func :PFviiE
bool type_index :1

这篇关于c++ typeid和type_index的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/869987

相关文章

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js