The string representation of a type is implementation defined in C++

The string representation of a type is implementation defined in C++, for example the following code produce the different output with MSVC, GCC and CLang:

#include <string>
#include <iostream>

struct A {};
class B {};

namespace ns
{
    struct X {};
}

int main()
{
    std::cout << typeid(A).name() << ", " << typeid(B).name() << ", " << typeid(ns::X).name() << ", " << typeid(std::string).name() << std::endl;
    return 0;
}

MSVC:

struct A, class B, struct ns::X, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >

GCC:

1A, 1B, N2ns1XE, NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

CLang:

1A, 1B, N2ns1XE, NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE

To compile the sample code with MSVC run the compiler with the following options:

cl /std:c++latest /EHsc a.cpp

Leave a Reply

Your email address will not be published. Required fields are marked *