为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如何让它打印未损坏的名称?

发布于 2024-10-07 16:17:00 字数 488 浏览 1 评论 0 原文

为什么当我运行这个 main.cpp 时:

#include <iostream>
#include <typeinfo>

using namespace std;

struct Blah {};

int main() {
  cout << typeid(Blah).name() << endl;
  return 0;
}

通过使用 GCC 版本 4.4.4 编译它:

g++ main.cpp

我得到这个:

4Blah

在 Visual C++ 2008 上,我会得到:

struct Blah

有没有办法让它只打印 <代码>Blah 还是struct Blah

How come when I run this 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:

g++ main.cpp

I get this:

4Blah

On Visual C++ 2008, I would get:

struct Blah

Is there a way to make it just print Blah or struct Blah?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怪我鬧 2024-10-14 16:17:00

name 的返回是实现定义的:甚至不需要实现为不同类型返回不同的字符串。

你从 g++ 得到的是一个 修饰名称,你可以使用 c++filt 命令或 __cxa_demangle

The return of name is implementation defined : an implementation is not even required to return different strings for different types.

What you get from g++ is a decorated name, that you can "demangle" using the c++filt command or __cxa_demangle.

埋情葬爱 2024-10-14 16:17:00

返回的字符串是实现定义的。

gcc 正在做的是返回损坏的名称。
您可以使用 c++filt 将损坏的名称转换为纯文本

> a.out | c++filt

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

> a.out | c++filt
情绪少女 2024-10-14 16:17:00

有没有办法让它只打印

Blah 还是struct Blah

否。std::typeinfo::name() 的结果未指定。它甚至可能为所有类型返回相同的字符串(或者实际上,为所有类型返回空字符串),并且实现仍然符合标准。您不能依赖其结果。确实,我发现它唯一有用的就是调试。

告诉我们您需要它做什么。通常,您可以使用特征来代替。

Is there a way to make it just print

Blah or 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.

Tell us what what you need it for. Often traits is what you use instead.

要走干脆点 2024-10-14 16:17:00

正如其他人所说,这里的结果是实现定义的,这意味着实现(即编译器工具链)可以自由地定义它,只要它在某处记录即可。

来自 C++ 标准,第 18.5.1/1 节 [lib.type.info]:

type_info描述了实现生成的类型信息。该类的对象
有效地存储指向类型名称的指针,以及适合比较两种类型的编码值
相等或整理顺序。类型的名称、编码规则和整理顺序均未指定
并且不同程序之间可能有所不同。

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.

From the C++ standard, section 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.

沫雨熙 2024-10-14 16:17:00

在 4Blah 中,4 是班级名称中的字母数。例如,如果您的类名称是 myEmptyClass 那么它将打印 12myEmptyClass。

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.

行雁书 2024-10-14 16:17:00

typeid().name() 取决于实现。它甚至可能为每种类型返回空字符串。这不会是非常有用的实现,但它是有效的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文