写了个 C++ 名字的 demangle 函数

当然不是自己解码的,不过有神人这么做:
http://hi.baidu.com/avengine/blog/item/9b7147a94c78c4fc1e17a2f7.html

还有这个
http://sourceforge.net/projects/php-ms-demangle/
是抄的 wine 的。

搞这个是为了解决 gcc typeinfo::name 返回的不是原始名字的问题。

gcc 输出:
St6vectorISsSaISsEE
std::vector<std::string, std::allocator<std::string> >

VC 的 typeinfo 的 name 是 demangle 后的,输出
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%s/n", name, CxxDemangle(name).c_str());
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值