typeid的输出

使用typeid获取的类型名是经过修饰的,并且如何修饰是实现定义的。要去掉修饰,我们可以使用c++filt__cxx_demangle

我们来看一个例子:

#include <iostream>
#include <cxxabi.h>
#include <typeinfo>
#include <cstdlib>

struct empty {};

template <typename T, size_t N> struct bar{};

int main() {
  int status;
  char* real_name;

  bar<empty, 17> b;
  const std::type_info& ti = typeid(b);
  real_name = abi::__cxa_demangle(ti.name(), 0, 0, &status);
  std::cout << ti.name() << " => " << real_name << " " << status << std::endl;
  free(real_name);
  return 0;
}

这个例子的输出为:3barI5emptyLm17EE => bar 0

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s