2 Copyright 2014 Peter Dimov
3 Copyright 2014 Andrey Semashev
5 Distributed under the Boost Software License, Version 1.0.
7 See accompanying file LICENSE_1_0.txt
8 or copy at http://boost.org/LICENSE_1_0.txt
11 [section:demangle demangle]
20 [section Header <boost/core/demangle.hpp>]
22 The header `<boost/core/demangle.hpp>` defines several tools for undecorating
32 std::string demangle( char const * name );
34 char const * demangle_alloc( char const * name ) noexcept;
35 void demangle_free( char const * demangled_name ) noexcept;
37 class scoped_demangled_name
40 explicit scoped_demangled_name( char const * name ) noexcept;
41 ~scoped_demangled_name() noexcept;
42 char const * get() const noexcept;
44 scoped_demangled_name( scoped_demangled_name const& ) = delete;
45 scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
53 [section Conventional interface]
55 The function `boost::core::demangle` is the conventional
56 way to obtain demangled symbol name. It takes a mangled string such as
57 those returned by `typeid(T).name()` on certain implementations
58 such as `g++`, and returns its demangled, human-readable, form. In case if
59 demangling fails (e.g. if `name` cannot be interpreted as a mangled name)
60 the function returns `name`.
64 #include <boost/core/demangle.hpp>
68 template<class T> struct X
74 char const * name = typeid( X<int> ).name();
76 std::cout << name << std::endl; // prints 1XIiE
77 std::cout << boost::core::demangle( name ) << std::endl; // prints X<int>
84 [section Low level interface]
86 In some cases more low level interface may be desirable. For example:
88 * Assuming that symbol demangling may fail, the user wants to be able to handle such errors.
89 * The user needs to post-process the demangled name (e.g. remove common namespaces), and
90 allocating a temporary string with the complete demangled name is significant overhead.
92 The function `boost::core::demangle_alloc` performs name demangling and returns a pointer
93 to a string with the demangled name, if succeeded, or `nullptr` otherwise. The returned pointer
94 must be passed to `boost::core::demangle_free` to reclaim resources. Note that on some platforms
95 the pointer returned by `boost::core::demangle_alloc` may refer to the string denoted by `name`,
96 so this string must be kept immutable for the whole life time of the returned pointer.
98 The `boost::core::scoped_demangled_name` class is a scope guard that automates the calls to
99 `boost::core::demangle_alloc` (on its construction) and `boost::core::demangle_free` (on destruction).
100 The string with the demangled name can be obtained with its `get` method. Note that this method may
101 return `nullptr` if demangling failed.
105 #include <boost/core/demangle.hpp>
109 template<class T> struct X
115 char const * name = typeid( X<int> ).name();
116 boost::core::scoped_demangled_name demangled( name );
118 std::cout << name << std::endl; // prints 1XIiE
119 std::cout << (demangled.get() ? demangled.get() : "[unknown]") << std::endl; // prints X<int>
128 [section Acknowledgments]
130 The implementation of `core::demangle` was taken from
131 `boost/exception/detail/type_info.hpp`, which in turn was adapted
132 from `boost/units/detail/utility.hpp` and `boost/log/utility/type_info_wrapper.hpp`.