我不明白为什么 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 拒绝该代码是正确的?
最佳答案
感谢@HowardHinnant 的评论,我设法解决了这个问题。问题在预处理器输出中变得很明显:libstdc++ 包含 <exception>
来自 <type_info>
甚至在它声明之前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现在已针对 GCC 4.7.3+ 修复
关于c++ - 尽管包含 <typeinfo> ,但 Clang 拒绝 type_info 为不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098174/