c++ - Are all functions "noexcept" if exceptions are disabled? -
if turn off exceptions compiling -fno-exceptions
functions considered noexcept example std::move_if_noexcept
or still have declare functions noexcept reason?
the -fno-exceptions
prevent throwing exceptions, can not prevent exceptions being thrown libraries.
for example, next example terminate because of not caught exception :
#include <vector> int main() { std::vector<int> v{1,2,3,4,5,6}; return v.at(55); }
but next example not compile, because of -fno-exceptions
option :
int main() { throw 22; }
it fails :
g++ -std=c++11 -g -wall -wextra -fno-exceptions ./garbage.cpp ./garbage.cpp: in function ‘int main()’: ./garbage.cpp:4:8: error: exception handling disabled, use -fexceptions enable throw 22;
from this article, doing without chapter :
user code uses c++ keywords throw, try, , catch produce errors if user code has included libstdc++ headers , using constructs basic_iostream.
on other hand, noexcept
marks method method doesn't throw exceptions. thrown exception call std::terminate
(see [except.terminate]/2 in c++ standard).
next example :
struct { void foo() noexcept { throw 33; } }; int main() { a; try { a.foo(); } catch(...) { } }
terminates :
terminate called after throwing instance of 'int' aborted (core dumped)
to conclude : behavior quite different when use -fno-exceptions
, when mark function noexcept
.
although compile whole project -fno-exceptions(for other reasons) still have declare move constructors move assigment operators noexcept enable move semantic std::move_if_noexcept?
when use option, functions not automatically marked noexcept. have manually. compiler not allowed such modifications.
if such modification allowed, this example produce different outputs.
Comments
Post a Comment