我不明白为什么 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>
from <type_info>
甚至在它之前declared 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++ 是否已经有一个错误报告,如果没有,则创建一个。
UPDATE: Bug #56468 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56468现已修复 GCC 4.7.3+
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)