🐭 类型推导结果查看方式
使用了 auto 与 decltype 之后,一些类型信息就对开发者「隐藏」了,当我们想还原其背后的确切类型时,大概有以下的一些辅助手段,本文重点介绍其中的 3 和 4:
- IDE 的智能提示
- 编译器的编译警告与编译错误
- 标准库的类型识别
- Boost 库增强型的类型识别
🐂 C++ 标准库的类型识别
类型操作比值操作要复杂得多,一般开发者不需要了解太多,对于库开发者和模板开发者来说,这个是比较重要的内容。通过类型操作,可以做很多复杂的不可思议的事情,比如模板元编程就需要大量使用类型操作技术。Type support (basic types, RTTI, type traits) https://en.cppreference.com/w/cpp/types
标准库运行时类型识别代码示例:
#include <iostream>void fun(int){};int main(void) {// std::type_info::name returns a string containing the name of the typeusing T = int;T t0{};T t1[2] = {{}, {}};std::cout << typeid(t0).name() << '\n'; // istd::cout << typeid(t1).name() << '\n'; // A2_iT &t2 = t0;const T &t3 = t0;T &&t4 = T{};std::cout << typeid(t2).name() << '\n'; // istd::cout << typeid(t3).name() << '\n'; // istd::cout << typeid(t4).name() << '\n'; // iT *t5 = &t0;const T *t6 = &t0;const T *const t7 = &t0;std::cout << typeid(t5).name() << '\n'; // Pistd::cout << typeid(t6).name() << '\n'; // PKistd::cout << typeid(t7).name() << '\n'; // PKiauto t8 = fun;std::cout << typeid(t8).name() << '\n'; // PFviEreturn 0;}
从上面各种类型的 typeid 输出结果来看,不直观,也不好理解。这个时候,我们还需要使用另外一个工具来帮助我们翻译这些奇怪的符号到真实的类型。
:::info
$ c++filt i A2_i Pi PKi PFviE
int
int [2]
int
int const
void (*)(int)
:::
另外,有些类型的 typeid 输出结果,似乎也不准确,比如 t3 和 t4,这是因为 typeinfo 是一个函数模板,类型是按值传入的,进行模板类型推导时类型的 cvref 修饰部分会被忽略掉。
:::tips
这里涉及到模板类型推导规则,在标准库 typeinfo 函数模板中的具体应用
:::
🐯 Boost 库增强型的类型识别
Boost 库中增强型的 typeid,能够保留类型的 cvref 修饰符,因而更准确,能返回变量或者表达式的完整类型。
Boost 库运行时类型识别代码示例:
#include <boost/type_index.hpp>#include <iostream>void fun(int){};int main(void) {// std::type_info::name returns a string containing the name of the type// full_typeid returns a string containing the full/real name of the type#define full_typeid(v) boost::typeindex::type_id_with_cvr<decltype(v)>()using T = int;T t0{};T t1[2] = {{}, {}};std::cout << typeid(t0).name() << '\n'; // istd::cout << typeid(t1).name() << '\n'; // A2_istd::cout << full_typeid(t0).pretty_name() << '\n'; // intstd::cout << full_typeid(t1).pretty_name() << '\n'; // int [2]T &t2 = t0;const T &t3 = t0;T &&t4 = T{};std::cout << typeid(t2).name() << '\n'; // istd::cout << typeid(t3).name() << '\n'; // istd::cout << typeid(t4).name() << '\n'; // istd::cout << full_typeid(t2).pretty_name() << '\n'; // int&std::cout << full_typeid(t3).pretty_name() << '\n'; // int const&std::cout << full_typeid(t4).pretty_name() << '\n'; // int &&T *t5 = &t0;const T *t6 = &t0;const T *const t7 = &t0;std::cout << typeid(t5).name() << '\n'; // Pistd::cout << typeid(t6).name() << '\n'; // PKistd::cout << typeid(t7).name() << '\n'; // PKistd::cout << full_typeid(t5).pretty_name() << '\n'; // int*std::cout << full_typeid(t6).pretty_name() << '\n'; // int const*std::cout << full_typeid(t7).pretty_name() << '\n'; // int const* constauto t8 = fun;std::cout << typeid(t8).name() << '\n'; // PFviEstd::cout << full_typeid(t8).pretty_name() << '\n'; // void (*)(int)return 0;}
:::tips Boost 库号称准标准库,里面有很多宝贝,值得去探究,C++ 标准库从 Boost 库也获益良多 :::
🐰 总结
- 使用一些辅助手段可以还原类型推导结果
- 熟练掌握类型推导规则同样很重要
