首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++中有__CLASS__宏吗?

C++中有__CLASS__宏吗?
EN

Stack Overflow用户
提问于 2009-11-03 19:42:45
回答 16查看 106K关注 0票数 119

C++中是否有一个__CLASS__宏,它给出的类名类似于__FUNCTION__宏,它给出的函数名

EN

Stack Overflow用户

发布于 2015-08-27 21:27:26

我想推荐boost::typeindex,这是我从Scott Meyer的“有效的现代C++”中学到的。下面是一个基本的例子:

示例

代码语言:javascript
复制
#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

票数 11
EN
查看全部 16 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1666802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档