[英]Does disabling support for exceptions also disable support for `std::move_if_noexcept`?
Some shops (eg, some video game development teams) disable support for exceptions in their build environment. 一些商店(例如,一些视频游戏开发团队)在其构建环境中禁用对异常的支持。 With exceptions disabled, developers would have no reason to declare their move operations
noexcept
(assuming that such code would even compile). 在禁用异常的情况下,开发人员没有理由声明他们的移动操作
noexcept
(假设这样的代码甚至可以编译)。 But standard library implementations are supposed to call std::move_if_noexcept
when implementing some operations (eg, std::vector::push_back
). 但是标准库实现应该在实现某些操作时调用
std::move_if_noexcept
(例如, std::vector::push_back
)。 Do standard library implementations typically check during compilation to see if exceptions are disabled and, if so, use std::move
instead of std::move_if_noexcept
? 标准库实现通常在编译期间检查以查看是否禁用了异常,如果是,则使用
std::move
而不是std::move_if_noexcept
? Do compilers cause std::is_nothrow_move_constructible
to return true for all types when exceptions are disabled? 当禁用异常时,编译器会导致
std::is_nothrow_move_constructible
为所有类型返回true吗? Or does disabling support for exceptions have the unexpected side effect of having std:move_if_noexcept
fail to enable move operations? 或者禁用异常支持会产生意外的副作用
std:move_if_noexcept
无法启用移动操作?
I'm interested in what happens in practice. 我对实践中发生的事情很感兴趣。 I understand that disabling support for exceptions takes us out of the realm of the C++ standard.
我知道禁用异常支持会使我们脱离C ++标准的范畴。
This code outputs false true false true
on both GCC 4.9 and clang 3.5 with or without exceptions enabled: 在启用或不启用异常的情况下,此代码在GCC 4.9和clang 3.5上输出
false true false true
:
void foo() {}
void bar() noexcept {}
void foo2() noexcept(noexcept(foo())) {}
void bar2() noexcept(noexcept(bar())) {}
int main() {
std::cout << std::boolalpha << noexcept(foo()) << ' ' << noexcept(bar())
<< ' ' << noexcept(foo2()) << ' ' << noexcept(bar2()) << std::endl;
}
So it looks like noexcept
behavior doesn't depend on compiler options at least for these two compilers. 所以看起来
noexcept
行为至少对这两个编译器来说并不依赖于编译器选项。
Update: VS2013 doesn't support noexcept
at all . 更新:VS2013 根本不支持
noexcept
。
-fnothrow-opt Treat a throw() exception specification as if it were a noexcept specification to reduce or eliminate the text size overhead relative to a function with no exception specification.
-fnothrow-opt将throw()异常规范视为noexcept规范,以减少或消除相对于没有异常规范的函数的文本大小开销。 If the function has local variables of types with non-trivial destructors, the exception specification actually makes the function smaller because the EH cleanups for those variables can be optimized away.
如果函数具有带有非平凡析构函数的类型的局部变量,则异常规范实际上使函数变小,因为可以优化这些变量的EH清理。 The semantic effect is that an exception thrown out of a function with such an exception specification results in a call to terminate rather than unexpected.
语义效果是从具有这种异常规范的函数抛出的异常导致调用终止而不是意外。
So according to the gcc documentation throw functions become noexcept specifications. 所以根据gcc文档的throw函数变成noexcept规范。
This should mean more objects will return true not less 这应该意味着更多的对象将返回true而不是更少
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.