C++ 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如何使其打印未混淆的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4465872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does typeid.name() return weird characters using GCC and how to make it print unmangled names?
提问by sivabudh
How come when I run this main.cpp
:
当我运行这个时怎么会main.cpp
:
#include <iostream>
#include <typeinfo>
using namespace std;
struct Blah {};
int main() {
cout << typeid(Blah).name() << endl;
return 0;
}
By compiling it with GCC version 4.4.4:
通过使用 GCC 版本 4.4.4 编译它:
g++ main.cpp
I get this:
我明白了:
4Blah
On Visual C++ 2008, I would get:
在 Visual C++ 2008 上,我会得到:
struct Blah
Is there a way to make it just print Blah
or struct Blah
?
有没有办法让它只是打印Blah
或struct Blah
?
回答by icecrime
The return of name
is implementation defined : an implementation is not even required to return different strings for different types.
的返回name
是实现定义的:一个实现甚至不需要为不同的类型返回不同的字符串。
What you get from g++ is a decorated name, that you can "demangle" using the c++filt
command or __cxa_demangle
.
你从 g++ 得到的是一个修饰的 name,你可以使用c++filt
命令或__cxa_demangle
.
回答by Martin York
The string returned is implementation defined.
返回的字符串是实现定义的。
What gcc is doing is returning the mangled name.
You can convert the mangled name into plain text with c++filt
gcc 正在做的是返回损坏的名称。
您可以使用 c++filt 将损坏的名称转换为纯文本
> a.out | c++filt
回答by sbi
Is there a way to make it just print
Blah
orstruct Blah
?
有没有办法让它只是打印
Blah
或者struct Blah
?
No. The result of std::typeinfo::name()
is unspecified. It might even return the same string for all types (or, indeed, empty strings for all types) and the implementation would still be standard-conforming. You must not rely on its result. Really, the only thing I found it useful for was debugging.
否。 的结果std::typeinfo::name()
未指定。它甚至可能为所有类型返回相同的字符串(或者,实际上,所有类型的空字符串)并且实现仍然符合标准。你不能依赖它的结果。真的,我发现它唯一有用的就是调试。
Tell us what what you need it for. Often traits is what you use instead.
告诉我们您需要它做什么。通常情况下,你使用的是traits。
回答by Adam Rosenfield
As others have said, the result here is implementation-defined, meaning that the implementation (i.e., the compiler toolchain) is free to define it how it wants, so long as it documents that somewhere.
正如其他人所说,这里的结果是implementation-defined,这意味着实现(即编译器工具链)可以自由地定义它想要的方式,只要它在某处记录。
From the C++ standard, section 18.5.1/1 [lib.type.info]:
来自 C++ 标准的第 18.5.1/1 节 [lib.type.info]:
The class
type_info
describes type information generated by the implementation. Objects of this class effectively store a pointer to a name for the type, and an encoded value suitable for comparing two types for equality or collating order. The names, encoding rule, and collating sequence for types are all unspecified and may differ between programs.
该类
type_info
描述了由实现生成的类型信息。此类的对象有效地存储指向类型名称的指针,以及适合比较两种类型的相等性或整理顺序的编码值。类型的名称、编码规则和整理顺序均未指定,并且可能因程序而异。
回答by Juraj Blaho
typeid().name()
is implementation dependent. It may even return empty string for every type. That would not be very useful implementation, but it would be valid.
typeid().name()
依赖于实现。它甚至可能为每种类型返回空字符串。这不会是非常有用的实现,但它会是有效的。
回答by Pradeep Rohilla
in 4Blah, 4 is the number of letters in your class name. For example if your class name is myEmptyClass then it would print 12myEmptyClass.
在 4Blah 中,4 是您班级名称中的字母数。例如,如果您的类名是 myEmptyClass,那么它将打印 12myEmptyClass。