简体   繁体   中英

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). But standard library implementations are supposed to call std::move_if_noexcept when implementing some operations (eg, 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 ? Do compilers cause std::is_nothrow_move_constructible to return true for all types when exceptions are disabled? Or does disabling support for exceptions have the unexpected side effect of having std:move_if_noexcept fail to enable move operations?

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.

This code outputs false true false true on both GCC 4.9 and clang 3.5 with or without exceptions enabled:

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.

Update: VS2013 doesn't support noexcept at all .

-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. 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. 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.

This should mean more objects will return true not less

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM