*: Regenerate.
[gcc.git] / libstdc++-v3 / doc / html / manual / ext_demangling.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 29. Demangling</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="
2 ISO C++
3 ,
4 library
5 "><meta name="keywords" content="
6 ISO C++
7 ,
8 runtime
9 ,
10 library
11 "><link rel="home" href="../index.html" title="The GNU C++ Library"><link rel="up" href="extensions.html" title="Part III.  Extensions"><link rel="prev" href="ext_io.html" title="Chapter 28. Input and Output"><link rel="next" href="ext_concurrency.html" title="Chapter 30. Concurrency"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 29. Demangling</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ext_io.html">Prev</a> </td><th width="60%" align="center">Part III. 
12 Extensions
13
14 </th><td width="20%" align="right"> <a accesskey="n" href="ext_concurrency.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 29. Demangling"><div class="titlepage"><div><div><h2 class="title"><a name="manual.ext.demangle"></a>Chapter 29. Demangling</h2></div></div></div><p>
15 Transforming C++ ABI identifiers (like RTTI symbols) into the
16 original C++ source identifiers is called
17 <span class="quote"><span class="quote">demangling.</span></span>
18 </p><p>
19 If you have read the <a class="link" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01115.html" target="_top">source
20 documentation for <code class="code">namespace abi</code></a> then you are
21 aware of the cross-vendor C++ ABI in use by GCC. One of the
22 exposed functions is used for demangling,
23 <code class="code">abi::__cxa_demangle</code>.
24 </p><p>
25 In programs like <span class="command"><strong>c++filt</strong></span>, the linker, and other tools
26 have the ability to decode C++ ABI names, and now so can you.
27 </p><p>
28 (The function itself might use different demanglers, but that's the
29 whole point of abstract interfaces. If we change the implementation,
30 you won't notice.)
31 </p><p>
32 Probably the only times you'll be interested in demangling at runtime
33 are when you're seeing <code class="code">typeid</code> strings in RTTI, or when
34 you're handling the runtime-support exception classes. For example:
35 </p><pre class="programlisting">
36 #include &lt;exception&gt;
37 #include &lt;iostream&gt;
38 #include &lt;cxxabi.h&gt;
39
40 struct empty { };
41
42 template &lt;typename T, int N&gt;
43 struct bar { };
44
45
46 int main()
47 {
48 int status;
49 char *realname;
50
51 // exception classes not in &lt;stdexcept&gt;, thrown by the implementation
52 // instead of the user
53 std::bad_exception e;
54 realname = abi::__cxa_demangle(e.what(), 0, 0, &amp;status);
55 std::cout &lt;&lt; e.what() &lt;&lt; "\t=&gt; " &lt;&lt; realname &lt;&lt; "\t: " &lt;&lt; status &lt;&lt; '\n';
56 free(realname);
57
58
59 // typeid
60 bar&lt;empty,17&gt; u;
61 const std::type_info &amp;ti = typeid(u);
62
63 realname = abi::__cxa_demangle(ti.name(), 0, 0, &amp;status);
64 std::cout &lt;&lt; ti.name() &lt;&lt; "\t=&gt; " &lt;&lt; realname &lt;&lt; "\t: " &lt;&lt; status &lt;&lt; '\n';
65 free(realname);
66
67 return 0;
68 }
69 </pre><p>
70 This prints
71 </p><pre class="screen">
72 <code class="computeroutput">
73 St13bad_exception =&gt; std::bad_exception : 0
74 3barI5emptyLi17EE =&gt; bar&lt;empty, 17&gt; : 0
75 </code>
76 </pre><p>
77 The demangler interface is described in the source documentation
78 linked to above. It is actually written in C, so you don't need to
79 be writing C++ in order to demangle C++. (That also means we have to
80 use crummy memory management facilities, so don't forget to free()
81 the returned char array.)
82 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ext_io.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extensions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ext_concurrency.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 28. Input and Output </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 30. Concurrency</td></tr></table></div></body></html>