1

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::stringconcatenations, 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?

Manu343726
  • 13,969
  • 4
  • 40
  • 75
  • 1
    version of gcc? gcc 4.8 compile this snippet fine. – ForEveR Sep 04 '13 at 07:52
  • 1
    Without a compilable example, I had to guess to put a file together, but having done so it compiled without error with `g++` 4.8.1. Please provide a complete example (I just want to be able to do an exact copy-paste-compile), and the exact command you are using to compile. Also, which gcc version may matter. And for kicks, which boost version. – BoBTFish Sep 04 '13 at 07:52
  • @BoBTFish Sorry. Im using g++ 4.8.1. Im going to edit the question to include the complete code. – Manu343726 Sep 04 '13 at 09:05

0 Answers0