6

我不知道为什么 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 -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.
$ 

问题:如何使用 Clang 修复它?还是我错过了什么,而 Clang 拒绝代码是正确的?

4

1 回答 1

5

感谢@HowardHinnant 的评论,我设法解决了这个问题。这个问题在预处理器输出中变得很明显:libstdc++甚至在它声明之前就包含<exception>了. 这使得 Clang 假设了一个新的 forward-declaration 。解决方案很简单,因为它是非法的:<type_info> std::type_infostd::__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++ 是否已经有一个错误报告,如果没有,创建一个。

更新:现在为 GCC 4.7.3+ 修复了错误# 56468

于 2013-02-26T20:36:46.843 回答