是否可以从C ++中的模板类型获取char *名称 [英] Is it possible to get a char* name from a template type in C++

查看:57
本文介绍了是否可以从C ++中的模板类型获取char *名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取模板类型的字符串名称(const char *).不幸的是,我无权使用RTTI.

I want to get the string name (const char*) of a template type. Unfortunately I don't have access to RTTI.

template< typename T >
struct SomeClass
{
    const char* GetClassName() const { return /* magic goes here */; }
}

所以

SomeClass<int> sc;
sc.GetClassName();   // returns "int"

这可能吗?我找不到办法,将要放弃.感谢您的帮助.

Is this possible? I can't find a way and am about to give up. Thanks for the help.

推荐答案

否,它也不能与typeid一起可靠地工作.它将为您提供一些取决于编译器实现的内部字符串.对于 int ,"int"之类的东西也很常见.

No and it will not work reliable with typeid either. It will give you some internal string that depends on the compiler implementation. Something like "int", but also "i" is common for int.

顺便说一句,如果您只想比较两种类型是否相同,则无需先将它们转换为字符串.你可以做

By the way, if what you want is to only compare whether two types are the same, you don't need to convert them to a string first. You can just do

template<typename A, typename B>
struct is_same { enum { value = false }; };

template<typename A>
struct is_same<A, A> { enum { value = true }; };

然后做

if(is_same<T, U>::value) { ... }

Boost已经有了这样的模板,下一个C ++标准也将具有 std :: is_same .

Boost already has such a template, and the next C++ Standard will have std::is_same too.

您可以专门研究以下类型:

You can specialize on the types like this:

template<typename> 
struct to_string {
    // optionally, add other information, like the size
    // of the string.
    static char const* value() { return "unknown"; }
};

#define DEF_TYPE(X) \
    template<> struct to_string<X> { \
        static char const* value() { return #X; } \
    }

DEF_TYPE(int); DEF_TYPE(bool); DEF_TYPE(char); ...

因此,您可以像使用它

char const *s = to_string<T>::value();

当然,如果您想在类型未知的情况下发生编译时错误,则也可以摆脱主要模板定义(仅保留前向声明).我只是将其包括在这里以完成.

Of course, you can also get rid of the primary template definition (and keep only the forward declaration) if you want to get a compile time error if the type is not known. I just included it here for completion.

我以前使用过char const *的静态数据成员,但是它们会引起一些复杂的问题,例如在哪里声明它们的声明等.像上面这样的班级专业化很容易解决这个问题.

I used static data-members of char const* previously, but they cause some intricate problems, like questions where to put declarations of them, and so on. Class specializations like above solve the issue easily.

另一种方法是依靠编译器内部.在GCC中,以下内容为我提供了合理的结果:

Another approach is to rely on compiler internals. In GCC, the following gives me reasonable results:

template<typename T>
std::string print_T() {
    return __PRETTY_FUNCTION__;
}

返回 std :: string .

std :: string print_T()[with T = std :: basic_string< char,std :: char_traits< char> ;, std :: allocator< char>>]

一些 substr 魔术与 find 混合在一起,将为您提供所需的字符串表示形式.

Some substr magic intermixed with find will give you the string representation you look for.

这篇关于是否可以从C ++中的模板类型获取char *名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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