c++ - std::throw_with_nested 需要 C++11 中的多态类型?

标签 c++ exception c++11 exception-handling libstdc++

为什么这不能编译(用 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));
      ^

C++11 中的 std::throw_with_nested 是否需要具有多态类型的参数,或者这是编译器或 libstdc++ 中的错误?

最佳答案

这是一个错误。

I implemented it对于 2009 年的 libstdc++,N2619 中的规范要求 E 为多态类型,但 2011 标准中的最终规范不同,libstdc++ 中的实现从未改变。

关于c++ - std::throw_with_nested 需要 C++11 中的多态类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25324262/

相关文章:

c++ - Assimp 无法在 Code::Blocks 中正确构建 - "TVITEMEXW not declared in current scope"

java - 为数组长度设置 ArrayIndexOutOfBound 异常

python - 处理异常的首选方法是什么?

C++ lambda 表达式(匿名函数)

c++11 - 对 'rank'的引用不明确

c++ - 跨多个文件的好友功能

c++ - 如何显示不带小数的整数

c++ - 等待条件的非线程替代方法。 (编辑 : Proactor pattern with boost. asio?)

java - 在android中使用异常

c++ - 我可以实例化一个 std::reference_wrapper<T> 吗,其中 T 是一个不完整的类型?