简体   繁体   English

禁用异常支持是否也禁用对`std :: move_if_noexcept`的支持?

[英]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;    
}

Demo 演示

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.

相关问题 std :: move_if_noexcept调用copy-assignment,即使move-assignment是noexcept; 为什么? - std::move_if_noexcept calls copy-assignment even though move-assignment is noexcept; why? 为什么在C ++ 11/14中没有std :: move_if_noexcept对应std :: forward? - Why there is no std::move_if_noexcept counterpart for std::forward in C++11/14? std :: move_if_noexcept的基本原理仍在移动投掷仅移动类型? - Rationale for std::move_if_noexcept still moving throwing move-only types? std :: is_nothrow_move_constructible是否需要一个noexcept析构函数? - Does std::is_nothrow_move_constructible require a noexcept destructor? std :: future是否支持多态? - Does std::future support polymorphism? std :: map是否支持缓存? - Does std::map support caching? 如果没有 noexcept 移动构造函数,为什么带有 std::vector 的代码不能编译,但带有 std::unique_ptr 的代码却可以编译? - Why does code with std::vector not compile but with std::unique_ptr it does, if there is no noexcept move constructor? 为什么没有std :: uninitialized_move_if_noexcept? - Why there is no std::uninitialized_move_if_noexcept? 为什么 std::map 的移动构造函数不是 noexcept? - Why is std::map's move constructor not noexcept? 为什么std :: vector使用移动构造函数,尽管声明为noexcept(false) - Why does std::vector use the move constructor although declared as noexcept(false)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM