首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::throw_with_nested期望C++11中的多态类型?

std::throw_with_nested期望C++11中的多态类型?
EN

Stack Overflow用户
提问于 2014-08-15 09:38:01
回答 3查看 605关注 0票数 12

为什么不编译(用Clang 3.4.2和GCC版本尝试4.7.4、4.8.3和4.9.1):

代码语言:javascript
复制
#include <exception>

struct E { E(int) {} };

int main() {
  std::throw_with_nested(E(42));
  return 0;
}

GCC的错误4.9.1:

代码语言:javascript
复制
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中的错误:

代码语言:javascript
复制
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?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-08-15 10:02:38

是个窃听器。

2009年,当我实现了 for libstdc++时,N2619中的规范要求E是多态类型,但是2011年标准中的最终规范是不同的,在libstdc++中的实现从未改变过。

票数 16
EN

Stack Overflow用户

发布于 2014-08-15 09:44:35

这似乎是个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派生的,是一个未指定类型的例外,它公开地从Unested_exception派生,并从std::forward<T>(t)构造,否则是std::forward<T>(t)。 §18.8.6 except.nested

票数 8
EN

Stack Overflow用户

发布于 2014-08-15 10:02:48

它看起来确实像一个bug (参见其他答案),ref§18.8.6/7。如果不是已经从std::nested_exception派生的,则使用来自Enested_exception的未指定类型。

作为一种建议中的,在修复的同时,明确地将派生出来,或者将析构函数实现为虚拟

代码语言:javascript
复制
#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&) {
 }
}

样品这里.

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25324262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档