typeid的(T).nameOfTheBeast() [英] typeid(T).nameOfTheBeast()

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

问题描述

有时候,在一种内省的情绪中,我的代码反思自己,我喜欢用b
写测试代码来打印内省的结果,其中一些

包含类型的名称。问题是:typeid(T).name(),在我的

通常的世界中(例如gcc版本3.2.2 [FreeBSD] 20030205(发布)),返回

一个难以辨认的破坏或一个神秘的缩写。例如:


typeid(unsigned int).name()


返回j。虽然我很欣赏所涉及的节省,但它仍然会激怒读者的输出 - 而不是j。他们希望看到

" unsigned int"。麻烦!


所以我想到了:


模板< typename T>

struct TName

{

static const char * Is()

{

返回typeid(T).name();

}

};


模板<>

struct TName< bool>

{

static const char * Is(){return" bool" ;; }

};


....


模板<>

struct TName< long double>

{

static const char * Is(){return" long double" ;; }

};


那些想要玩得很好的本土类型做到这一点:


模板<> ; struct TName< AClass>

{static const char * Is(){return" AClass" ;;什么更好的想法?

Sometimes, in an introspective mood, my code reflecting on itself, I like to
write test code that prints the results of introspection, some of which
consist of the names of Types. The problem is: typeid(T).name(), in my
usual worlds (e.g. gcc version 3.2.2 [FreeBSD] 20030205 (release)), returns
an illegible mangling or a cryptic abbreviation. For example:

typeid(unsigned int).name()

returns "j". And while I do appreciate the savings involved it nonetheless
irritates readers of the output - rather than "j" they would like to see
"unsigned int". Bother!

So I had a thought:

template<typename T>
struct TName
{
static const char* Is()
{
return typeid(T).name();
}
};

template<>
struct TName<bool>
{
static const char* Is() { return "bool"; }
};

....

template<>
struct TName<long double>
{
static const char* Is() { return "long double"; }
};

And those homegrown Types that want to play nice do this:

template<> struct TName<AClass>
{ static const char* Is() { return "AClass"; } };

Any better ideas?

推荐答案

Michael Olea写道:
Michael Olea wrote:
有时候,在一种内省的情绪中,我的代码反思自己,我喜欢编写打印内省结果的测试代码,其中一些由类型名称组成。问题是:typeid(T).name(),在我的通常世界中(例如gcc版本3.2.2 [FreeBSD] 20030205(发布)),返回
一个难以辨认的错误或一个神秘的缩写。例如:

typeid(unsigned int).name()
Sometimes, in an introspective mood, my code reflecting on itself, I like to
write test code that prints the results of introspection, some of which
consist of the names of Types. The problem is: typeid(T).name(), in my
usual worlds (e.g. gcc version 3.2.2 [FreeBSD] 20030205 (release)), returns
an illegible mangling or a cryptic abbreviation. For example:

typeid(unsigned int).name()



为什么不使用重载函数,例如


const char *是(bool b){return" bool";}

const char * is(const long double& ld){return" long double" ;;}


模板< typename T>

const char *是(const T& t){return typeid(T).name();}

void foo(){

bool test;

cout<< 当前类型是 <<是(测试)<< endl;

cout<< 当前类型是 <<是(std :: string())<< endl;

}


Why not just use overloaded functions, e.g.

const char* is (bool b) { return "bool";}
const char* is (const long double& ld) { return "long double";}

template <typename T>
const char* is (const T& t) { return typeid(T).name();}
void foo() {
bool test;
cout << "current type is " << is(test) << endl;
cout << "current type is " << is(std::string()) << endl;
}


Michael Olea写道:
Michael Olea wrote:
有时,在内省的心情,我的代码反思自己,我喜欢编写打印内省结果的测试代码,其中一些由类型名称组成。问题是:typeid(T).name(),在我的通常世界中(例如gcc版本3.2.2 [FreeBSD] 20030205(发布)),
返回一个难以辨认的错误或一个神秘的缩写。例如:

typeid(unsigned int).name()

返回j。虽然我很欣赏所涉及的节省,但它仍然会激怒读者的输出 - 而不是j。他们希望看到
unsigned int。麻烦!
Sometimes, in an introspective mood, my code reflecting on itself, I like
to write test code that prints the results of introspection, some of which
consist of the names of Types. The problem is: typeid(T).name(), in my
usual worlds (e.g. gcc version 3.2.2 [FreeBSD] 20030205 (release)),
returns an illegible mangling or a cryptic abbreviation. For example:

typeid(unsigned int).name()

returns "j". And while I do appreciate the savings involved it nonetheless
irritates readers of the output - rather than "j" they would like to see
"unsigned int". Bother!




好​​吧,如果你可以在这里使用编译器特定的功能,那么你可以取消该名称的
。 g ++使用与名称修改中使用的相同的字符串

。查看cxxabi.h头文件。它提供了一个

函数abi :: __ cxa_demangle(),它可以将

typeid(T).name()返回的名称转换成你想要的名字。


xpost& f''up2 gnu.g ++。帮助,因为该组显然更适合我的帖子

答案。



Well, if you can live with using compiler specific functionality here, then
you could demangle that name. g++ uses just the same string that would be
used in name mangling. Look into the cxxabi.h header. It provides a
function abi::__cxa_demangle() that can convert the name returned by
typeid(T).name() into what you want.

xpost&f''up2 gnu.g++.help, because that group is clearly more appropriate for
answers to my posting.


Michael Olea写道:
Michael Olea wrote:
有时候,在一种内省的情绪中,我的代码反思自己,我喜欢编写打印结果的测试代码内省,其中一些由类型名称组成。问题是:typeid(T).name(),在我的通常世界中(例如gcc版本3.2.2 [FreeBSD] 20030205(发布)),返回
一个难以辨认的错误或一个神秘的缩写。例如:

typeid(unsigned int).name()

返回j。虽然我很欣赏所涉及的节省,但它仍然会激怒读者的输出 - 而不是j。他们希望看到
unsigned int。麻烦!
Sometimes, in an introspective mood, my code reflecting on itself, I like to
write test code that prints the results of introspection, some of which
consist of the names of Types. The problem is: typeid(T).name(), in my
usual worlds (e.g. gcc version 3.2.2 [FreeBSD] 20030205 (release)), returns
an illegible mangling or a cryptic abbreviation. For example:

typeid(unsigned int).name()

returns "j". And while I do appreciate the savings involved it nonetheless
irritates readers of the output - rather than "j" they would like to see
"unsigned int". Bother!




GNU实用程序c ++ filt,它解码符号名称,是一个包装器

来自binutils的库函数(libbfd) ?)其原型我

不能立即在/ usr / include中找到。它需要一个受损的符号名称

,如_Z23frobnicateRK213_fooBlah并将它们变成`string

frobnicate(unsigned int,struct foo)'';我相信它正在做你需要获得参数类型的
。但是我不记得它有一个

的公共界面只是为了进行demangling类型。


在/ usr / include中的grepping显示了一些名为__cxa_demangle的东西在

/usr/include/c++/3.3/cxxabi.h。不确定这是怎么相关的。


如果一切都失败我肯定binutils的来源,gl for mangl,将会b / b
显示答案。


--Phil。



The GNU utility c++filt, which demangles symbol names, is a wrapper
around a library function from binutils (libbfd?) whose prototype I
can''t immediately find in /usr/include. It takes mangled symbol names
like _Z23frobnicateRK213_fooBlah and turns them into `string
frobnicate(unsigned int, struct foo)''; I believe that it is doing what
you need to get the parameter types. However I don''t recall it having a
public interface for just doing the type demangling.

grepping in /usr/include has revealed something called __cxa_demangle in
/usr/include/c++/3.3/cxxabi.h. Not sure how that is related.

If all else fails I''m sure the binutils source, grepped for mangl, will
reveal the answer.

--Phil.


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

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