Reputation: 58
I am working on a pretty dynamic C++ program which allows the user to define their own data structures which are then serialized in an output HDF5 data file. Instead of requiring the user to define a new HDF5 data type, I am "splitting" their data structures into HDF5 subgroups in which I store the different member variable data sets. I am interested in labeling the HDF5 group that has the subgroup members with the type of the data structure that was written to it so that future users of the data file will have more knowledge about how to use the data contained within it.
All of this context gets me to my question in the title. How reliable are demangled names? The crux of the issue could be summarized with the following example (using boost
to demangle as an example, not a necessity). If I use
std::string tn = boost::core::demangle(typeid(MyType).name());
to get the demangled name of MyType
on one system with a given compiler, will I get the same result if I use the same code on a different system with a potentially different compiler? Could I safely do tn_sys_with_clang == tn_sys_with_gcc
and trust that this equality holds as long as MyType
is the same type?
The answer seems to me to be obviously yes, and I have checked a few examples across many different compilers on Compiler Explorer; however, I want to be confident that I am not missing any edge cases. Moreover, I'm not sure how the demangling process differs between compilers and how that might lead to the introduction of differing amounts of whitespace.
The emphasis here is that the only variable changing is the system and compiler. I know that changing the definition of MyType
or moving it to a different namespace or including a pesky using
directive or changing how I demangle could change the string output by the demangling. I want to focus on a more limited question where only the compiler and system change.
Upvotes: 2
Views: 447
Reputation: 10048
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...”
Upvotes: 1
Reputation: 58
The reliability of de-mangled names does not seem to be something that is well documented. For this reason, I am going to simply document the few tests that I've done on my x86_64 system allowing me to compare gcc and clang. These tests done through Compiler Explorer verifies that the returned strings for the same types are the same (including whitespace).
Maybe if I start using this in my application, one of the users will find an issue and I can update this question with another answer down the line, but for now, I think it is safe(ish) to trust de-mangling.
Upvotes: 0