Write a C ++ name demangle Function

Source: Internet
Author: User

Of course it is not decoded by yourself, but some gods do this:

Http://hi.baidu.com/avengine/blog/item/9b7147a94c78c4fc1e17a2f7.html

And this

Http://sourceforge.net/projects/php-ms-demangle/

It is copied from wine.

This is to solve the problem that GCC typeinfo: name does not return the original name.

GCC output:
St6vectorisssaissee
STD: vector <STD: String, STD: Allocator <STD: String>

The typeinfo name of VC is after demangle, and the output is
Class STD: vector <class STD: basic_string <char, struct STD: char_traits <char>,
Class STD: Allocator <char>, class STD: Allocator <class STD: basic_string <char,
Struct STD: char_traits <char>, class STD: Allocator <char >>>
Class STD: vector <class STD: basic_string <char, struct STD: char_traits <char>,
Class STD: Allocator <char>, class STD: Allocator <class STD: basic_string <char,
Struct STD: char_traits <char>, class STD: Allocator <char >>>

# Include <typeinfo>

# Ifdef _ msc_ver

# Ifndef win32_lean_and_mean
# Define win32_lean_and_mean
# Endif

# Ifndef _ win32_winnt
# DEFINE _ win32_winnt 0x0501
# Endif

# Include <windows. h>
# Include <dbghelp. h>

STD: String cxxdemangle (const char * name)
{
Char buffer [1024];
DWORD length = undecoratesymbolname (name, buffer, sizeof (buffer), 0 );
If (length> 0)
Return STD: string (buffer, length );
Else
Return name;
}

# Pragma comment (Lib, "dbghelp ")

# Elif defined _ gnuc __

# Include <cxxabi. h>

STD: String cxxdemangle (const char * name)
{
Char buffer [1024];
Size_t size = sizeof (buffer );
Int status;
If (ABI ::__ cxa_demangle (name, buffer, & size, & Status ))
Return STD: string (buffer, size );
Else
Return name;
}

# Endif

# Include <vector>
# Include <stdio. h>
# Include <map>
# Include <set>

Int main ()
{
Const char * name = typeid (STD: vector <STD: String>). Name ();
Printf ("% s/n", name, cxxdemangle (name). c_str ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.