table_of_names.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2013-2019 Antony Polukhin
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See the accompanying file LICENSE_1_0.txt
  4. // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. //[type_index_names_table
  6. /*`
  7. The following example shows how different type names look when we explicitly use classes for RTTI and RTT off.
  8. This example requires RTTI. For a more portable example see 'Getting human readable and mangled type names':
  9. */
  10. #include <boost/type_index/stl_type_index.hpp>
  11. #include <boost/type_index/ctti_type_index.hpp>
  12. #include <iostream>
  13. template <class T>
  14. void print(const char* name) {
  15. boost::typeindex::stl_type_index sti = boost::typeindex::stl_type_index::type_id<T>();
  16. boost::typeindex::ctti_type_index cti = boost::typeindex::ctti_type_index::type_id<T>();
  17. std::cout << "\t[" /* start of the row */
  18. << "[" << name << "]"
  19. << "[`" << sti.raw_name() << "`] "
  20. << "[`" << sti.pretty_name() << "`] "
  21. << "[`" << cti.raw_name() << "`] "
  22. << "]\n" /* end of the row */ ;
  23. }
  24. struct user_defined_type{};
  25. namespace ns1 { namespace ns2 {
  26. struct user_defined_type{};
  27. }} // namespace ns1::ns2
  28. namespace {
  29. struct in_anon_type{};
  30. } // anonymous namespace
  31. namespace ns3 { namespace { namespace ns4 {
  32. struct in_anon_type{};
  33. }}} // namespace ns3::{anonymous}::ns4
  34. template <class T0, class T1>
  35. class templ {};
  36. template <>
  37. class templ<int, int> {};
  38. int main() {
  39. std::cout << "[table:id Table of names\n";
  40. std::cout << "\t[[Type] [RTTI & raw_name] [RTTI & pretty_name] [noRTTI & raw_name]]\n";
  41. print<user_defined_type>("User defined type");
  42. print<in_anon_type>("In anonymous namespace");
  43. print<ns3::ns4::in_anon_type>("In ns3::{anonymous}::ns4 namespace");
  44. print<templ<short, int> >("Template class");
  45. print<templ<int, int> >("Template class (full specialization)");
  46. print<templ<
  47. templ<char, signed char>,
  48. templ<int, user_defined_type>
  49. > >("Template class with templae classes");
  50. std::cout << "]\n";
  51. }
  52. /*`
  53. Code from the example will produce the following table:
  54. [table:id Table of names
  55. [[Type] [RTTI & raw_name] [RTTI & pretty_name] [noRTTI & raw_name]]
  56. [[User defined type][`17user_defined_type`] [`user_defined_type`] [`user_defined_type]`] ]
  57. [[In anonymous namespace][`N12_GLOBAL__N_112in_anon_typeE`] [`(anonymous namespace)::in_anon_type`] [`{anonymous}::in_anon_type]`] ]
  58. [[In ns3::{anonymous}::ns4 namespace][`N3ns312_GLOBAL__N_13ns412in_anon_typeE`] [`ns3::(anonymous namespace)::ns4::in_anon_type`] [`ns3::{anonymous}::ns4::in_anon_type]`] ]
  59. [[Template class][`5templIsiE`] [`templ<short, int>`] [`templ<short int, int>]`] ]
  60. [[Template class (full specialization)][`5templIiiE`] [`templ<int, int>`] [`templ<int, int>]`] ]
  61. [[Template class with template classes][`5templIS_IcaES_Ii17user_defined_typeEE`] [`templ<templ<char, signed char>, templ<int, user_defined_type> >`] [`templ<templ<char, signed char>, templ<int, user_defined_type> >]`] ]
  62. ]
  63. We have not show the "noRTTI & pretty_name" column in the table because it is almost equal
  64. to "noRTTI & raw_name" column.
  65. [warning With RTTI off different classes with same names in anonymous namespace may collapse. See 'RTTI emulation limitations'. ]
  66. */
  67. //] [/type_index_names_table]