]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/type_index/examples/demangled_names.cpp
448d4855bbf26ed6c2dd1bdc2043af62bf01daa7
[ceph.git] / ceph / src / boost / libs / type_index / examples / demangled_names.cpp
1 // Copyright 2013-2019 Antony Polukhin
2
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>.)
6
7
8 //[type_index_names_example
9 /*`
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.
12 */
13
14
15 #include <boost/type_index.hpp>
16 #include <iostream>
17
18 template <class T>
19 void foo(T) {
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();
22 }
23
24 struct user_defined_type{};
25
26 namespace ns1 { namespace ns2 {
27 struct user_defined_type{};
28 }} // namespace ns1::ns2
29
30 namespace {
31 struct in_anon_type{};
32 } // anonymous namespace
33
34 int main() {
35 // Call to
36 foo(1);
37 // will output something like this:
38 //
39 // (RTTI on) (RTTI off)
40 // Short name: i Short name: int]
41 // Readable name: int Readable name: int
42
43 user_defined_type t;
44 foo(t);
45 // Will output:
46 //
47 // (RTTI on) (RTTI off)
48 // Short name: 17user_defined_type user_defined_type]
49 // Readable name: user_defined_type user_defined_type
50
51 ns1::ns2::user_defined_type t_in_ns;
52 foo(t_in_ns);
53 // Will output:
54 //
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
58
59 in_anon_type anon_t;
60 foo(anon_t);
61 // Will output:
62 //
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
66 }
67
68 /*`
69 Short names are very compiler dependant: some compiler will output `.H`, others `i`.
70
71 Readable names may also differ between compilers: `struct user_defined_type`, `user_defined_type`.
72
73 [warning With RTTI off different classes with same names in anonymous namespace may collapse. See 'RTTI emulation limitations'. ]
74 */
75
76 //] [/type_index_names_example]