1 // Copyright 2013-2019 Antony Polukhin
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See the accompanying file LICENSE_1_0.txt
5 // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
8 //[type_index_names_example
10 The following example shows how short (mangled) and human readable type names could be obtained from a type.
11 Works with and without RTTI.
15 #include <boost/type_index.hpp>
20 std::cout
<< "\n Short name: " << boost::typeindex::type_id
<T
>().raw_name();
21 std::cout
<< "\n Readable name: " << boost::typeindex::type_id
<T
>().pretty_name();
24 struct user_defined_type
{};
26 namespace ns1
{ namespace ns2
{
27 struct user_defined_type
{};
28 }} // namespace ns1::ns2
31 struct in_anon_type
{};
32 } // anonymous namespace
37 // will output something like this:
39 // (RTTI on) (RTTI off)
40 // Short name: i Short name: int]
41 // Readable name: int Readable name: int
47 // (RTTI on) (RTTI off)
48 // Short name: 17user_defined_type user_defined_type]
49 // Readable name: user_defined_type user_defined_type
51 ns1::ns2::user_defined_type t_in_ns
;
55 // (RTTI on) (RTTI off)
56 // Short name: N3ns13ns217user_defined_typeE ns1::ns2::user_defined_type]
57 // Readable name: ns1::ns2::user_defined_type ns1::ns2::user_defined_type
63 // (RTTI on) (RTTI off)
64 // Short name: N12_GLOBAL__N_112in_anon_typeE {anonymous}::in_anon_type]
65 // Readable name: (anonymous namespace)::in_anon_type {anonymous}::in_anon_type
69 Short names are very compiler dependant: some compiler will output `.H`, others `i`.
71 Readable names may also differ between compilers: `struct user_defined_type`, `user_defined_type`.
73 [warning With RTTI off different classes with same names in anonymous namespace may collapse. See 'RTTI emulation limitations'. ]
76 //] [/type_index_names_example]