Inn*_*der 9 c++ g++ c++11 clang++ libc++
这是非常基本的代码:
#include <memory>
class foo
{
public:
~foo() noexcept(false) { }
};
int main()
{
auto x = std::make_shared<foo>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译如下:
g++ -std=c++11 test.cpp <-- OK
clang++ -std=c++11 test.cpp <-- OK
clang++ -std=c++11 -stdlib=libc++ test.cpp <-- FAIL
Run Code Online (Sandbox Code Playgroud)
使用libc ++编译时,它会失败:
/usr/bin/../include/c++/v1/memory:3793:7: error: exception specification of overriding function is more lax than base version
class __shared_ptr_emplace
^
/usr/bin/../include/c++/v1/memory:4423:26: note: in instantiation of template class 'std::__1::__shared_ptr_emplace<foo,
std::__1::allocator<foo> >' requested here
::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
^
/usr/bin/../include/c++/v1/memory:4787:29: note: in instantiation of function template specialization
'std::__1::shared_ptr<foo>::make_shared<>' requested here
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
^
exc.cpp:11:19: note: in instantiation of function template specialization 'std::__1::make_shared<foo>' requested here
auto x = std::make_shared<foo>();
^
/usr/bin/../include/c++/v1/memory:3719:13: note: overridden virtual function is here
virtual ~__shared_weak_count();
Run Code Online (Sandbox Code Playgroud)
我认为它可能是libc ++中的一个错误,但是在我提交错误之前想在这里查看.
问题归结为:
正如 @TC 所说,[res.on.functions]/2 指出:
在某些情况下(替换函数、处理函数、用于实例化标准库模板组件的类型的操作),C++ 标准库依赖于 C++ 程序提供的组件。如果这些组件不满足其要求,则本国际标准对实施不提出任何要求。
特别是,在以下情况下效果未定义:
[跳过]
-- 如果任何替换函数或处理函数或析构函数操作通过异常退出,除非在适用的必需行为:段落中明确允许。
抛开标准术语不谈,在很长一段时间里(至少自 C++98 以来),从析构函数中抛出异常一直是一个坏主意。如果运行中出现异常,并且在堆栈展开期间抛出另一个异常,则可以快速访问std::terminate().
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |