1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 28. Demangling</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><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 27. Input and Output" /><link rel="next" href="ext_concurrency.html" title="Chapter 29. Concurrency" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 28. 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. 3 Extensions 4 5</th><td width="20%" align="right"> <a accesskey="n" href="ext_concurrency.html">Next</a></td></tr></table><hr /></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a id="manual.ext.demangle"></a>Chapter 28. Demangling</h2></div></div></div><p> 6 Transforming C++ ABI identifiers (like RTTI symbols) into the 7 original C++ source identifiers is called 8 <span class="quote">“<span class="quote">demangling.</span>”</span> 9 </p><p> 10 If you have read the <a class="link" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaces.html" target="_top">source 11 documentation for <code class="code">namespace abi</code></a> then you are 12 aware of the cross-vendor C++ ABI in use by GCC. One of the 13 exposed functions is used for demangling, 14 <code class="code">abi::__cxa_demangle</code>. 15 </p><p> 16 In programs like <span class="command"><strong>c++filt</strong></span>, the linker, and other tools 17 have the ability to decode C++ ABI names, and now so can you. 18 </p><p> 19 (The function itself might use different demanglers, but that's the 20 whole point of abstract interfaces. If we change the implementation, 21 you won't notice.) 22 </p><p> 23 Probably the only times you'll be interested in demangling at runtime 24 are when you're seeing <code class="code">typeid</code> strings in RTTI, or when 25 you're handling the runtime-support exception classes. For example: 26 </p><pre class="programlisting"> 27#include <exception> 28#include <iostream> 29#include <cxxabi.h> 30 31struct empty { }; 32 33template <typename T, int N> 34 struct bar { }; 35 36 37int main() 38{ 39 int status; 40 char *realname; 41 42 // typeid 43 bar<empty,17> u; 44 const std::type_info &ti = typeid(u); 45 46 realname = abi::__cxa_demangle(ti.name(), 0, 0, &status); 47 std::cout << ti.name() << "\t=> " << realname << "\t: " << status << '\n'; 48 free(realname); 49 50 return 0; 51} 52 </pre><p> 53 This prints 54 </p><pre class="screen"> 55 <code class="computeroutput"> 56 3barI5emptyLi17EE => bar<empty, 17> : 0 57 </code> 58 </pre><p> 59 The demangler interface is described in the source documentation 60 linked to above. It is actually written in C, so you don't need to 61 be writing C++ in order to demangle C++. (That also means we have to 62 use crummy memory management facilities, so don't forget to 63 <code class="code">free()</code> the returned char array.) 64 </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 27. 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 29. Concurrency</td></tr></table></div></body></html>