🐭 类型推导结果查看方式

使用了 auto 与 decltype 之后,一些类型信息就对开发者「隐藏」了,当我们想还原其背后的确切类型时,大概有以下的一些辅助手段,本文重点介绍其中的 3 和 4:

  1. IDE 的智能提示
  2. 编译器的编译警告与编译错误
  3. 标准库的类型识别
  4. Boost 库增强型的类型识别

    🐂 C++ 标准库的类型识别

    类型操作比值操作要复杂得多,一般开发者不需要了解太多,对于库开发者和模板开发者来说,这个是比较重要的内容。通过类型操作,可以做很多复杂的不可思议的事情,比如模板元编程就需要大量使用类型操作技术。

    Type support (basic types, RTTI, type traits) https://en.cppreference.com/w/cpp/types

标准库运行时类型识别代码示例:

  1. #include <iostream>
  2. void fun(int){};
  3. int main(void) {
  4. // std::type_info::name returns a string containing the name of the type
  5. using T = int;
  6. T t0{};
  7. T t1[2] = {{}, {}};
  8. std::cout << typeid(t0).name() << '\n'; // i
  9. std::cout << typeid(t1).name() << '\n'; // A2_i
  10. T &t2 = t0;
  11. const T &t3 = t0;
  12. T &&t4 = T{};
  13. std::cout << typeid(t2).name() << '\n'; // i
  14. std::cout << typeid(t3).name() << '\n'; // i
  15. std::cout << typeid(t4).name() << '\n'; // i
  16. T *t5 = &t0;
  17. const T *t6 = &t0;
  18. const T *const t7 = &t0;
  19. std::cout << typeid(t5).name() << '\n'; // Pi
  20. std::cout << typeid(t6).name() << '\n'; // PKi
  21. std::cout << typeid(t7).name() << '\n'; // PKi
  22. auto t8 = fun;
  23. std::cout << typeid(t8).name() << '\n'; // PFviE
  24. return 0;
  25. }

从上面各种类型的 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 库运行时类型识别代码示例:

  1. #include <boost/type_index.hpp>
  2. #include <iostream>
  3. void fun(int){};
  4. int main(void) {
  5. // std::type_info::name returns a string containing the name of the type
  6. // full_typeid returns a string containing the full/real name of the type
  7. #define full_typeid(v) boost::typeindex::type_id_with_cvr<decltype(v)>()
  8. using T = int;
  9. T t0{};
  10. T t1[2] = {{}, {}};
  11. std::cout << typeid(t0).name() << '\n'; // i
  12. std::cout << typeid(t1).name() << '\n'; // A2_i
  13. std::cout << full_typeid(t0).pretty_name() << '\n'; // int
  14. std::cout << full_typeid(t1).pretty_name() << '\n'; // int [2]
  15. T &t2 = t0;
  16. const T &t3 = t0;
  17. T &&t4 = T{};
  18. std::cout << typeid(t2).name() << '\n'; // i
  19. std::cout << typeid(t3).name() << '\n'; // i
  20. std::cout << typeid(t4).name() << '\n'; // i
  21. std::cout << full_typeid(t2).pretty_name() << '\n'; // int&
  22. std::cout << full_typeid(t3).pretty_name() << '\n'; // int const&
  23. std::cout << full_typeid(t4).pretty_name() << '\n'; // int &&
  24. T *t5 = &t0;
  25. const T *t6 = &t0;
  26. const T *const t7 = &t0;
  27. std::cout << typeid(t5).name() << '\n'; // Pi
  28. std::cout << typeid(t6).name() << '\n'; // PKi
  29. std::cout << typeid(t7).name() << '\n'; // PKi
  30. std::cout << full_typeid(t5).pretty_name() << '\n'; // int*
  31. std::cout << full_typeid(t6).pretty_name() << '\n'; // int const*
  32. std::cout << full_typeid(t7).pretty_name() << '\n'; // int const* const
  33. auto t8 = fun;
  34. std::cout << typeid(t8).name() << '\n'; // PFviE
  35. std::cout << full_typeid(t8).pretty_name() << '\n'; // void (*)(int)
  36. return 0;
  37. }

:::tips Boost 库号称准标准库,里面有很多宝贝,值得去探究,C++ 标准库从 Boost 库也获益良多 :::

🐰 总结

  1. 使用一些辅助手段可以还原类型推导结果
  2. 熟练掌握类型推导规则同样很重要