2

我正在开发一个非常动态的 C++ 程序,它允许用户定义自己的数据结构,然后在输出HDF5数据文件中序列化这些数据结构。我没有要求用户定义新的 HDF5 数据类型,而是将他们的数据结构“拆分”为 HDF5 子组,在其中存储不同的成员变量数据集。我有兴趣使用写入其中的数据结构类型标记具有子组成员的 HDF5 组,以便数据文件的未来用户将更多地了解如何使用其中包含的数据。

所有这些背景都让我想到了标题中的问题。去杂化的名称有多可靠?问题的症结可以用下面的例子来概括(boost以 demangle 为例,不是必须的)。如果我使用

std::string tn = boost::core::demangle(typeid(MyType).name());

要在具有给定编译器的一个系统上获取 demangled 名称,MyType如果我在具有可能不同编译器的不同系统上使用相同的代码,我会得到相同的结果吗?tn_sys_with_clang == tn_sys_with_gcc只要MyType是同一类型,我可以安全地相信这种相等性吗?

在我看来,答案显然是肯定的,我在 Compiler Explorer 上检查了许多不同编译器的几个示例;但是,我想确信我没有遗漏任何边缘情况。此外,我不确定编译器之间的拆解过程有何不同,以及这如何导致引入不同数量的空白。

这里的重点是唯一改变的变量是系统和编译器。我知道更改定义MyType或将其移动到不同的命名空间或包含讨厌的using指令或更改我的 demangle 可能会更改 demangle 的字符串输出。我想关注一个更有限的问题,只有编译器和系统会发生变化。

4

2 回答 2

1

Unreliable.

If you compile with the same compiler on the same OS then you should have some stability — but that is absolutely not guaranteed. ABI changes in name mangling can happen at any time in a compiler’s release cycle.

Individual compiler teams may have some information about this in their documentation. I am not going to look it up. Sorry.

All bets are off if you compile with either different compilers or different operating systems.

For example, LLVM/Clang on Windows comes with a version that uses MSVC as the backend. Consequently, name mangling on the native Windows Clang port is not compatible with the native Linux Clang.

Finally, just running a few tests with your (current) compiler is always a good way to shoot yourself in the foot. As the adage goes, “just because it works on your compiler, today...”</p>

于 2022-01-27T17:10:17.287 回答
0

去污名的可靠性似乎没有得到很好的记录。出于这个原因,我将简单地记录我在 x86_64 系统上所做的一些测试,以便比较 gcc 和 clang。这些通过 Compiler Explorer 完成的测试验证返回的相同类型的字符串是否相同(包括空格)。

也许如果我开始在我的应用程序中使用它,其中一个用户会发现一个问题,我可以用另一个答案更新这个问题,但是现在,我认为信任 de-mangling 是安全的(ish)。

于 2022-01-27T16:57:38.147 回答