<?xml version="1.0" encoding="UTF-8" standalone="no"?><!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Β 40.Β Demangling</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="extensions.html" title="PartΒ XII.Β Extensions" /><link rel="prev" href="ext_io.html" title="ChapterΒ 39.Β Input and Output" /><link rel="next" href="ext_concurrency.html" title="ChapterΒ 41.Β Concurrency" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">ChapterΒ 40.Β 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Β XII.ΒExtensions</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Β 40.Β Demangling"><div class="titlepage"><div><div><h2 class="title"><a id="manual.ext.demangle"></a>ChapterΒ 40.Β Demangling</h2></div></div></div><p>Transforming C++ ABI identifiers (like RTTI symbols) into theoriginal C++ source identifiers is called<span class="quote">β<span class="quote">demangling.</span>β</span></p><p>If you have read the <a class="ulink" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01115.html" target="_top">sourcedocumentation for <code class="code">namespace abi</code></a> then you areaware of the cross-vendor C++ ABI in use by GCC. One of theexposed functions is used for demangling,<code class="code">abi::__cxa_demangle</code>.</p><p>In programs like <span class="command"><strong>c++filt</strong></span>, the linker, and other toolshave the ability to decode C++ ABI names, and now so can you.</p><p>(The function itself might use different demanglers, but that's thewhole point of abstract interfaces. If we change the implementation,you won't notice.)</p><p>Probably the only times you'll be interested in demangling at runtimeare when you're seeing <code class="code">typeid</code> strings in RTTI, or whenyou're handling the runtime-support exception classes. For example:</p><pre class="programlisting">#include <exception>#include <iostream>#include <cxxabi.h>struct empty { };template <typename T, int N>struct bar { };int main(){int status;char *realname;// exception classes not in <stdexcept>, thrown by the implementation// instead of the userstd::bad_exception e;realname = abi::__cxa_demangle(e.what(), 0, 0, &status);std::cout << e.what() << "\t=> " << realname << "\t: " << status << '\n';free(realname);// typeidbar<empty,17> u;const std::type_info &ti = typeid(u);realname = abi::__cxa_demangle(ti.name(), 0, 0, &status);std::cout << ti.name() << "\t=> " << realname << "\t: " << status << '\n';free(realname);return 0;}</pre><p>This prints</p><pre class="screen"><code class="computeroutput">St13bad_exception => std::bad_exception : 03barI5emptyLi17EE => bar<empty, 17> : 0</code></pre><p>The demangler interface is described in the source documentationlinked to above. It is actually written in C, so you don't need tobe writing C++ in order to demangle C++. (That also means we have touse crummy memory management facilities, so don't forget to free()the returned char array.)</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Β 39.Β Input and OutputΒ </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top">Β ChapterΒ 41.Β Concurrency</td></tr></table></div></body></html>