Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Given the attention this question / answer receives, and the valuable feedback from <a href="https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname/4541470#comment26629807_4541470">GManNickG</a>, I have cleaned up the code a little bit. Two versions are given: one with C++11 features and another one with only C++98 features.</p> <p>In file <strong>type.hpp</strong></p> <pre class="lang-cpp prettyprint-override"><code>#ifndef TYPE_HPP #define TYPE_HPP #include &lt;string&gt; #include &lt;typeinfo&gt; std::string demangle(const char* name); template &lt;class T&gt; std::string type(const T&amp; t) { return demangle(typeid(t).name()); } #endif </code></pre> <p>In file <strong>type.cpp</strong> (requires C++11)</p> <pre class="lang-cpp prettyprint-override"><code>#include "type.hpp" #ifdef __GNUG__ #include &lt;cstdlib&gt; #include &lt;memory&gt; #include &lt;cxxabi.h&gt; std::string demangle(const char* name) { int status = -4; // some arbitrary value to eliminate the compiler warning // enable c++11 by passing the flag -std=c++11 to g++ std::unique_ptr&lt;char, void(*)(void*)&gt; res { abi::__cxa_demangle(name, NULL, NULL, &amp;status), std::free }; return (status==0) ? res.get() : name ; } #else // does nothing if not g++ std::string demangle(const char* name) { return name; } #endif </code></pre> <p>Usage:</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt; #include "type.hpp" struct Base { virtual ~Base() {} }; struct Derived : public Base { }; int main() { Base* ptr_base = new Derived(); // Please use smart pointers in YOUR code! std::cout &lt;&lt; "Type of ptr_base: " &lt;&lt; type(ptr_base) &lt;&lt; std::endl; std::cout &lt;&lt; "Type of pointee: " &lt;&lt; type(*ptr_base) &lt;&lt; std::endl; delete ptr_base; } </code></pre> <p>It prints:</p> <p>Type of ptr_base: <code>Base*</code><br> Type of pointee: <code>Derived</code></p> <p>Tested with g++ 4.7.2, g++ 4.9.0 20140302 (experimental), clang++ 3.4 (trunk 184647), clang 3.5 (trunk 202594) on Linux 64 bit and g++ 4.7.2 (Mingw32, Win32 XP SP2).</p> <p>If you cannot use C++11 features, here is how it can be done in C++98, the file <strong>type.cpp</strong> is now:</p> <pre class="lang-cpp prettyprint-override"><code>#include "type.hpp" #ifdef __GNUG__ #include &lt;cstdlib&gt; #include &lt;memory&gt; #include &lt;cxxabi.h&gt; struct handle { char* p; handle(char* ptr) : p(ptr) { } ~handle() { std::free(p); } }; std::string demangle(const char* name) { int status = -4; // some arbitrary value to eliminate the compiler warning handle result( abi::__cxa_demangle(name, NULL, NULL, &amp;status) ); return (status==0) ? result.p : name ; } #else // does nothing if not g++ std::string demangle(const char* name) { return name; } #endif </code></pre> <hr> <hr> <p><em>(Update from Sep 8, 2013)</em></p> <p><a href="https://stackoverflow.com/a/281876/341970">The accepted answer (as of Sep 7, 2013)</a>, when the call to <code>abi::__cxa_demangle()</code> is successful, <strong>returns a pointer to a local, stack allocated array</strong>... ouch!<br> Also note that if you provide a buffer, <code>abi::__cxa_demangle()</code> assumes it to be allocated on the heap. Allocating the buffer on the stack is a bug (from the gnu doc): <em>"If <code>output_buffer</code> is not long enough, it is expanded using <code>realloc</code>."</em> <strong>Calling <code>realloc()</code> on a pointer to the stack</strong>... ouch! (See also <a href="https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname#comment16312718_4541470">Igor Skochinsky</a>'s kind comment.) </p> <p>You can easily verify both of these bugs: just reduce the buffer size in the accepted answer (as of Sep 7, 2013) from 1024 to something smaller, for example 16, and give it something with a name <em>not</em> longer than 15 (so <code>realloc()</code> is <em>not</em> called). Still, depending on your system and the compiler optimizations, the output will be: garbage / nothing / program crash.<br> To verify the second bug: set the buffer size to 1 and call it with something whose name is longer than 1 character. When you run it, the program almost assuredly crashes as it attempts to call <code>realloc()</code> with a pointer to the stack.</p> <hr> <p><em>(The old answer from Dec 27, 2010)</em></p> <p>Important changes made to <a href="https://stackoverflow.com/a/281876/341970">KeithB's code</a>: <strong>the buffer has to be either allocated by malloc or specified as NULL.</strong> Do NOT allocate it on the stack.</p> <p>It's wise to check that status as well.</p> <p>I failed to find <code>HAVE_CXA_DEMANGLE</code>. I check <code>__GNUG__</code> although that does not guarantee that the code will even compile. Anyone has a better idea?</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;cxxabi.h&gt; const string demangle(const char* name) { int status = -4; char* res = abi::__cxa_demangle(name, NULL, NULL, &amp;status); const char* const demangled_name = (status==0)?res:name; string ret_val(demangled_name); free(res); return ret_val; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload