【发布时间】:2014-08-15 10:01:17
【问题描述】:
为什么这不能编译(尝试使用 Clang 3.4.2 和 GCC 版本 4.7.4、4.8.3 和 4.9.1):
#include <exception>
struct Base {
inline Base(int) {}
virtual void f() {}
};
struct Derived: virtual Base {
inline Derived() : Base(42) {}
};
int main() {
std::throw_with_nested(Derived());
return 0;
}
来自 GCC 4.9.1 的错误:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/exception:163:0,
from test.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h: In instantiation of 'std::_Nested_exception<_Except>::_Nested_exception(_Except&&) [with _Except = Derived]':
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:126:60: required from 'void std::__throw_with_nested(_Ex&&, ...) [with _Ex = Derived]'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:140:58: required from 'void std::throw_with_nested(_Ex) [with _Ex = Derived]'
test.cpp:13:35: required from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:81:45: error: no matching function for call to 'Base::Base()'
: _Except(static_cast<_Except&&>(__ex))
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:81:45: note: candidates are:
test.cpp:4:10: note: Base::Base(int)
inline Base(int) {}
^
test.cpp:4:10: note: candidate expects 1 argument, 0 provided
test.cpp:3:8: note: constexpr Base::Base(const Base&)
struct Base {
^
test.cpp:3:8: note: candidate expects 1 argument, 0 provided
test.cpp:3:8: note: constexpr Base::Base(Base&&)
test.cpp:3:8: note: candidate expects 1 argument, 0 provided
如果我省略 virtual 关键字,我不会收到任何错误。这是 GCC 和 Clang 或 libstd++ 错误,还是我做错了什么?
【问题讨论】:
-
如果你在类的声明中声明了一个成员,那么标记它们是多余的
inline:“在类定义中定义的函数是内联函数。” §7.1.2 [dcl.fct.spec] -
为什么要做虚拟继承呢?
-
@RobertAllanHenniganLeahy 是的,错过了结构,编辑了我的评论
标签: c++ exception c++11 exception-handling libstdc++