typeid(T).name()在c ++ 11中的替代? [英] typeid(T).name() alternative in c++11?

查看:690
本文介绍了typeid(T).name()在c ++ 11中的替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++ 11中有一个标准的方法来获取一个类的名称有一些模板黑魔法或动态与一些标准库函数?

Is there a standard way in c++11 to get the name of a class either with some template black magic or dynamically with some standard library function?

推荐答案

一旦我发现函数原型的漂亮打印很有用:

Once I found the pretty printing of the function prototype quite useful:

在GCC中, PRETTY_FUNCTION 类型签名的函数以及其裸名。

In GCC, PRETTY_FUNCTION contains the type signature of the function as well as its bare name.

例如,对于模板化的类或函数,您可以将类名扩展为C字符串:

For example for a templated class or function you get the class names expanded as a C-string:

template<typename T>
class Vector {
  void foo(int i) {
    cout << __PRETTY_FUNCTION__ << endl;
  }
};

这会给你像

void Vector<double>::foo(int)

你已经实例化类,例如双打。但它也提供了用户定义的类型。

If you had instantiated the class with, e.g. doubles. But it gives you also user defined types.

不是很奇怪,但它有它的用途。

Not very fancy, but it has its uses.

这篇关于typeid(T).name()在c ++ 11中的替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆