C++中是否有一个__CLASS__宏,它给出的类名类似于__FUNCTION__宏,它给出的函数名
发布于 2015-08-27 21:27:26
我想推荐boost::typeindex,这是我从Scott Meyer的“有效的现代C++”中学到的。下面是一个基本的例子:
示例
#include <boost/type_index.hpp>
class foo_bar
{
int whatever;
};
namespace bti = boost::typeindex;
template <typename T>
void from_type(T t)
{
std::cout << "\tT = " << bti::type_id_with_cvr<T>().pretty_name() << "\n";
}
int main()
{
std::cout << "If you want to print a template type, that's easy.\n";
from_type(1.0);
std::cout << "To get it from an object instance, just use decltype:\n";
foo_bar fb;
std::cout << "\tfb's type is : "
<< bti::type_id_with_cvr<decltype(fb)>().pretty_name() << "\n";
}使用"g++ --std=c++14“进行编译会产生以下结果
输出
如果你想打印一个模板类型,这很容易。
T=双精度
要从对象实例中获取它,只需使用decltype:
fb类型为: foo_bar
https://stackoverflow.com/questions/1666802
复制相似问题