Question

I'm lost as to why Clang rejects the following code:

#include <typeinfo>
#include <exception>

const char* get_name( const std::exception_ptr eptr )
{
  return eptr.__cxa_exception_type()->name();
}

int main() {}

It OK with GCC, but Clang complains about type_info being an incomplete type:

$ g++-4.7 -std=c++0x -O3 -Wall -Wextra t.cc -o t
$ clang++-3.2 -std=c++0x -O3 -Wall -Wextra t.cc -o t
t.cc:6:37: error: member access into incomplete type 'const class type_info'
  return eptr.__cxa_exception_type()->name();
                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/exception_ptr.h:144:19: note: forward declaration of
      'std::__exception_ptr::type_info'
      const class type_info*
                  ^
1 error generated.
$ 

Question: How do I fix it with Clang? Or am I missing something and Clang is right to reject the code?

Was it helpful?

Solution

Thanks to @HowardHinnant's comment, I managed to fix the problem. The problem became obvious in the preprocessor output: libstdc++ includes <exception> from <type_info> before it even declared std::type_info. That made Clang assume a new forward-declaration std::__exception_ptr::type_info. The solution is as simple as it is illegal:

namespace std { class type_info; }

#include <typeinfo>
#include <exception>

const char* get_name( const std::exception_ptr eptr )
{
  return eptr.__cxa_exception_type()->name();
}

int main() {}

Seems like I should check if libstdc++ already has a bug report for that and, if not, create one.

UPDATE: Bug #56468 is now fixed for GCC 4.7.3+

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top