C++ libstdc&x2B+;std::throw_with_nested()需要多态类型
考虑以下(我认为是非法的)代码:C++ libstdc&x2B+;std::throw_with_nested()需要多态类型,c++,gcc,c++11,clang,libc++,C++,Gcc,C++11,Clang,Libc++,考虑以下(我认为是非法的)代码: #包括 #包括 使用名称空间std; int main() { 试一试{ 试一试{ 抛弦(“x”); } 捕捉(字符串和x) { 用嵌套的(字符串(“xx”))抛出_; } } 捕获(…) { 自动ep=当前_异常(); } 返回0; } 使用针对libc++编译的clang++编译此代码 使用g++对抗libstdc++: In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/excepti
#包括
#包括
使用名称空间std;
int main()
{
试一试{
试一试{
抛弦(“x”);
}
捕捉(字符串和x)
{
用嵌套的(字符串(“xx”))抛出_;
}
}
捕获(…)
{
自动ep=当前_异常();
}
返回0;
}
使用针对libc++编译的clang++编译此代码
使用g++对抗libstdc++:
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/exception:153:0,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h: In instantiation of �static const std::nested_exception* std::__get_nested_helper<_Ex>::_S_get(const _Ex&) [with _Ex = std::basic_string<char>]�:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:104:51: required from �const std::nested_exception* std::__get_nested_exception(const _Ex&) [with _Ex = std::basic_string<char>]�
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:138:38: required from �void std::throw_with_nested(_Ex) [with _Ex = std::basic_string<char>]�
main.cpp:15:43: required from here
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:90:59: error: cannot dynamic_cast �& __ex� (of type �const class std::basic_string<char>*�) to type �const class std::nested_exception*� (source type is not polymorphic)
{ return dynamic_cast<const nested_exception*>(&__ex); }
^
在/usr/local/gcc-4.8.1/include/c++/4.8.1/exception:153:0中包含的文件中,
来自main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:在–的实例化中��静态常量std::nested_exception*std::_get_nested_helper::_S_get(const_Ex&)[带_Ex=std::basic_string]��:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:104:51:必须来自��const std::nested_exception*std::uu get_nested_exception(const_Ex&)[带Ex=std::basic_string]��
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:138:38:required from–中的��void std::throw_with_嵌套(_Ex)[with _Ex=std::basic_string]��
main.cpp:15:43:从这里开始需要
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/nested_exception.h:90:59:error:cannot dynamic_cast��& __前�� (属于–类型)��常量类std::基本字符串*��) 键入��常量类std::嵌套的_异常*�� (源类型不是多态的)
{return dynamic_cast(&__ex);}
^
据我所知,std::throw_with_nested
要求T是多态的,而std::string不是多态的
这里有什么可以提供的吗
N3337[嵌套的除外]/5,6
[[noreturn]] template <class T> void throw_with_nested(T&& t);
[[noreturn]]模板void throw_与_嵌套(T&&T);
让U
beremove\U reference::type
5.要求:
U
应为
CopyConstructible
6.抛出:if
U
是非联合类类型,而不是
派生自nested_exception
,一种未指定类型的异常,公开派生自U
和nested_exception
,并由std::forward(t)
构造,否则为std::forward(t)
也认为
nested_异常
有一个虚拟析构函数使其成为
多态类。它的存在可以通过动态\u cast
进行测试。
-[完注]
实际抛出的异常类型始终是多态的。U
不一定是多态的,但是-正如Requires部分所指出的那样,只是CopyConstructible
因此libstdc++的实现无效。它应该在内部专门化是::value
是false
的类型
N3337[嵌套的除外]/5,6
[[noreturn]] template <class T> void throw_with_nested(T&& t);
[[noreturn]]模板void throw_与_嵌套(T&&T);
让U
beremove\U reference::type
5.要求:
U
应为
CopyConstructible
6.抛出:if
U
是非联合类类型,而不是
派生自nested_exception
,一种未指定类型的异常,公开派生自U
和nested_exception
,并由std::forward(t)
构造,否则为std::forward(t)
也认为
nested_异常
有一个虚拟析构函数使其成为
多态类。它的存在可以通过动态\u cast
进行测试。
-[完注]
实际抛出的异常类型始终是多态的。U
不一定是多态的,但是-正如Requires部分所指出的那样,只是CopyConstructible
因此libstdc++的实现无效。它应该在内部专门化是::value
是false
的类型
N3337[嵌套的除外]/5,6
[[noreturn]] template <class T> void throw_with_nested(T&& t);
[[noreturn]]模板void throw_与_嵌套(T&&T);
让U
beremove\U reference::type
5.要求:
U
应为
CopyConstructible
6.抛出:if
U
是非联合类类型,而不是
派生自nested_exception
,一种未指定类型的异常,公开派生自U
和nested_exception
,并由std::forward(t)
构造,否则为std::forward(t)
也认为
nested_异常
有一个虚拟析构函数使其成为
多态类。它的存在可以通过动态\u cast
进行测试。
-[完注]
实际抛出的异常类型始终是多态的。U
不一定是多态的,但是-正如Requires部分所指出的那样,只是CopyConstructible
因此libstdc++的实现无效。它应该在内部专门化是::value
是false
的类型
N3337[嵌套的除外]/5,6
[[noreturn]] template <class T> void throw_with_nested(T&& t);
[[noreturn]]模板void throw_与_嵌套(T&&T);
让U
beremove\U reference::type
5.要求:
U
应为
CopyConstructible
6.抛出:if
U
是非联合类类型,而不是
派生自nested\U exception
,一种未指定类型的异常,公开派生自U
和nes