Clang reject type_info as incomplete although <typeinfo> is included

这一生的挚爱 提交于 2019-12-22 05:09:38

问题


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?


回答1:


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+



来源:https://stackoverflow.com/questions/15098174/clang-reject-type-info-as-incomplete-although-typeinfo-is-included

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!