猿问

C ++中是否有__CLASS__宏?

__CLASS__C ++中是否有一个宏,该宏的类名类似于__FUNCTION__提供函数名的宏



catspeake
浏览 494回答 3
3回答

一只斗牛犬

我想建议我从Scott Meyer的“ Effective Modern C ++”中学到的boost :: typeindex,这是一个基本示例:例#include <boost/type_index.hpp>class foo_bar{&nbsp; &nbsp; int whatever;};namespace bti =&nbsp; boost::typeindex;template <typename T>void from_type(T t){&nbsp; &nbsp; std::cout << "\tT = " << bti::type_id_with_cvr<T>().pretty_name() << "\n";}int main(){&nbsp; &nbsp; std::cout << "If you want to print a template type, that's easy.\n";&nbsp; &nbsp; from_type(1.0);&nbsp; &nbsp; std::cout << "To get it from an object instance, just use decltype:\n";&nbsp; &nbsp; foo_bar fb;&nbsp; &nbsp; std::cout << "\tfb's type is : "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; << bti::type_id_with_cvr<decltype(fb)>().pretty_name() << "\n";}编译为“ g ++ --std = c ++ 14”会产生以下结果输出量如果要打印模板类型,这很容易。T =两倍要从对象实例获取它,只需使用decltype:fb的类型是:foo_bar
随时随地看视频慕课网APP
我要回答