std::throw_with_nested期望c++ 11中的多态类型
std::throw_with_nested expects polymorphic type in C++11?
为什么不能编译(在Clang 3.4.2和GCC 4.7.4, 4.8.3和4.9.1版本中尝试过):
#include <exception>
struct E { E(int) {} };
int main() {
std::throw_with_nested(E(42));
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 'static const std::nested_exception* std::__get_nested_helper<_Ex>::_S_get(const _Ex&) [with _Ex = E]':
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:104:51: required from 'const std::nested_exception* std::__get_nested_exception(const _Ex&) [with _Ex = E]'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:138:38: required from 'void std::throw_with_nested(_Ex) [with _Ex = E]'
test.cpp:6:31: required from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:90:59: error: cannot dynamic_cast '& __ex' (of type 'const struct E*') to type 'const class std::nested_exception*' (source type is not polymorphic)
{ return dynamic_cast<const nested_exception*>(&__ex); }
^
Clang 3.4.2:
In file included from test.cpp:1:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/exception:163:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:90:16: error: 'E' is not polymorphic
{ return dynamic_cast<const nested_exception*>(&__ex); }
^ ~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:104:40: note: in instantiation of member function 'std::__get_nested_helper<E>::_S_get' requested here
{ return __get_nested_helper<_Ex>::_S_get(__ex); }
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.1/include/g++-v4/bits/nested_exception.h:138:11: note: in instantiation of function template specialization 'std::__get_nested_exception<E>' requested here
if (__get_nested_exception(__ex))
^
test.cpp:6:8: note: in instantiation of function template specialization 'std::throw_with_nested<E>' requested here
std::throw_with_nested(E(42));
^
std::throw_with_nested
在c++ 11期望一个参数与多态类型或这是一个错误在编译器或libstdc++?
这是一个bug。
当我在2009年为libstdc++实现它时,N2619的规范要求E
是多态类型,但2011年标准中的最终规范不同,libstdc++中的实现从未改变。
这可能是一个bug。
标准规定std::throw_with_nested
:
[[noreturn]] template <class T> void throw_with_nested(T&& t);
设
U
为remove_reference<T>::type.
要求:
U
应为CopyConstructible
。抛出:如果
U
不是从nested_exception
派生的非联合类类型,则抛出一个未指定类型的异常,该异常从U
和nested_exception
公开派生,并从std::forward<T>(t)
构造,否则为std::forward<T>(t)
。§18.8.6 [except.nested]
它确实看起来像一个bug(参见其他答案),参考§18.8.6/7。如果尚未从std::nested_exception
派生,则使用从E
和nested_exception
派生的未指定类型。
作为建议的绕过的工作,当它被修复时,显式地从std::nested_exception
派生或将析构函数实现为虚的;
#include <exception>
struct E : std::nested_exception { E(int) {} };
// Alternative workaround... virtual destructor
// struct E { E(int) {} virtual ~E() {} };
int main() {
try {
std::throw_with_nested(E(42));
return 0;
}
catch (E&) {
}
}
样本。- 对于多态类型T,如何在没有类型T实例的情况下获得指向T的虚拟表的指针
- 哈希多态类型的正确方式
- C++ 2D shared_ptr数组使用抽象多态类型初始化
- 一个类似 std::访问的函数,用于访问多态类型
- 将unique_ptr返回到多态类型
- 为什么删除的复制构造函数不允许使用其他具有多态类型的构造函数?
- Sean Parent:对于继承层次结构中的多态类型,具有可变对象是极端的例外
- 通过指向非多态类型的基类的指针获取已分配内存的地址
- 如何在代码块 IDE 上修复此警告,警告:按值 [-Wcatch-value=] 捕获多态类型"类 std::d omain_error"
- 不是多态类型,或者为什么我们在这里需要虚拟析构函数?
- 游戏引擎:Dynamic_cast-对象类不是多态类型
- 使用decltype的动态多态类型演绎
- 编译器如何知道为多态类型选择哪个函数
- 多态类型的连续存储
- 如何在gSOAP中绑定多态类型
- 防止谷物写入多态类型数据
- C++传递函数作为具有多态类型的参数
- 返回不带动态内存分配的多态类型
- 符合标准的编译器是否可以拒绝包含非多态类型向下转换dynamic_cast代码
- 在循环中使用无变量多态类型进行优化