When I need to print type info at runtime, I always apply demangling to std::type_info::name()
result. This is the implementation for GCC, which uses abi::__cxa_demangle()
:
#include <cxxabi.h>
GCC demangling implementation
std::string demangle( const std::string& name )
{
int status;
return std::string( abi::__cxa_demangle( name.c_str() , 0 , 0 , &status ) );
return name;
}
Today I was writting a to_string
template which allows me to print the content of a typelist. So to avoid std::string
concatenations, I used a string stream, std::ostringstream
:
template<typename T>
struct to_string_t
{
operator std::string()
{
return demangle( typeid( T ).name() );
}
};
template<typename... Ts>
struct to_string_t<mpl::list<Ts...>>
{
operator std::string()
{
std::ostringstream os;
os << '[' << _to_string<mpl::list<Ts...>>() << ']';
return os.str();
}
};
_to_string
is a class template which implements operator<<
to print recursively the content of the typelist to the stream. (I don't include it to not bloat the post with non-related metaprogramming code).
This works perfectly without demangling. When I include <cxxabi>
to implement the demangling, the compiler shows an ambiguous reference to __gnu_gxx namespace
error in sstream.h
.
What could be the reason?