Clang拒绝type_info为不完整,尽管< typeinfo>已经包括了 [英] Clang reject type_info as incomplete although <typeinfo> is included

查看:101
本文介绍了Clang拒绝type_info为不完整,尽管< typeinfo>已经包括了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Clang为何拒绝以下代码感到困惑:

  #include< typeinfo> 
#include< exception>

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

int main(){}

可以与GCC,但Clang抱怨 type_info 是不完整的类型:

  $ g ++-4.7 -std = c ++ 0x -O3 -Wall -Wextra t.cc -ot 
$ clang ++-3.2 -std = c ++ 0x -O3 -Wall -Wall -Wextra t.cc -ot
t.cc:6:37:错误:成员访问不完整类型'const class type_info'
返回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:注意:
'std :: __ exception_ptr :: type_info'的前声明
const class type_info *
^
1生成错误。
$

问题:如何使用Clang进行修复?还是我错过了某些东西,而Clang拒绝代码是正确的?

解决方案

由于@HowardHinnant的评论,我设法解决了问题。该问题在预处理器输出中变得很明显:libstdc ++甚至包括了< exception> 来自< type_info> 。 em>已声明 std :: type_info 。这使得Clang假定了新的前向声明 std :: __ exception_ptr :: type_info 。解决方案非常简单,因为它是非法的:

  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(){}

似乎我应该检查libstdc ++是否已经有错误报告,如果没有,请创建一个。



更新:错误#56468

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?

解决方案

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+

这篇关于Clang拒绝type_info为不完整,尽管&lt; typeinfo&gt;已经包括了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆