I am playing with demangling and I noticed that for std::string the mangled name depends on the compiler in use. For example, this simple program:
#include <cxxabi.h>
#include <iostream>
#include <string>
int main(){
std::cout << typeid(std::string).name() << std::endl;
return 0;
}
compiled under CentOS 7 using g++ 7.3.1 (from devtoolset-7) with --std=c++14
returns:
Ss
while under Ubuntu 18.04 with g++ 7.5.0 with the same flag:
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
I don’t understand what determines the different behavior, since the two compiler versions are pretty similar. The only thing that comes to my mind is that maybe it could be due to different versions of libstdc++, but I can’t figure out the underlying reason.
2
Answers
That’s completely expected.
typeid
names are implementation defined and are not required to be identical across different implementations or even between different versions of the same implementation (compiler). They are also not required to be equal to the mangled names.The mangled names are decoded as follows:
Ss
is::std::basic_string<char,::std::char_traits<char>,::std::allocator<char>>
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
is::std::__cxx11::basic_string<char,::std::char_traits<char>,::std::allocator<char>>
The second type is in a different namespace. This is due to how libstdc++ managed the C++11 ABI break for
std::string
.The C++ standard does not say what the actual type of
std::string
is, and there is no ABI specification for this either, so there may be inline namespaces or typedefs involved when you writestd::string
.