If you turn off exceptions by compiling with -fno-exceptions
are all functions considered noexcept for example by std::move_if_noexcept
or do you still have to declare functions noexcept for that reason?
-
Lots of functions [may still throw exceptions](http://stackoverflow.com/questions/6049563/with-fno-exceptions-what-happens-with-new-t) despite compiling with `-fno-exceptions` – Praetorian Feb 06 '14 at 08:25
-
http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html doesn't say much about it, I'm not sure if that's the definitive reference though. – ta.speot.is Feb 06 '14 at 08:28
-
4Someone has [asked](http://comments.gmane.org/gmane.comp.compilers.clang.devel/30992) pretty much the same question on the clang mailing list, and the answer given is *compiling with `-fno-exceptions` is not the same as specifying `noexcept` for all functions*. – Praetorian Feb 06 '14 at 08:45
-
`-f` options affect code generation, not types. – n. m. could be an AI Feb 06 '14 at 10:04
1 Answers
The -fno-exceptions
will prevent you from throwing exceptions, but it can not prevent exceptions being thrown from libraries.
For example, next example will 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 will not compile, because of -fno-exceptions
option :
int main()
{
throw 22;
}
It fails with :
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 to enable
throw 22;
From this article, Doing without chapter :
User code that uses C++ keywords like throw, try, and catch will produce errors even if the user code has included libstdc++ headers and is using constructs like basic_iostream.
On the other hand, noexcept
marks the method as a method that doesn't throw exceptions. Any thrown exception will call std::terminate
(see [except.terminate]/2 in c++ standard).
Next example :
struct A
{
void foo() noexcept
{
throw 33;
}
};
int main()
{
A a;
try
{
a.foo();
}
catch(...)
{
}
}
terminates with :
terminate called after throwing an instance of 'int'
Aborted (core dumped)
To conclude : the behavior is quite different when you use -fno-exceptions
and when you mark the function as noexcept
.
Although I compile my whole project with -fno-exceptions(for other reasons) I still have to declare move constructors an move assigment operators noexcept to enable move semantic for std::move_if_noexcept?
When you use that option, the functions are not automatically marked as noexcept. You have to do it manually. The compiler is not allowed to do such modifications.
If such modification would be allowed, then this example would produce different outputs.

- 62,405
- 41
- 173
- 273
-
I know, that the behavior is quite different. My Question ist: Although I compile my whole project with `-fno-exceptions`(for other reasons) I still have to declare move constructors an move assigment operators noexcept to enable move semantic for std::move_if_noexcept? – TNA Feb 06 '14 at 10:25
-
1
-
@BЈовић Can you please add a reliable source for your claim that adding `-fno-exceptions` to the compiler options is not enough to trigger the same performance optimizations as manually marking all functions `noexcept`? Right now your supporting arguments are a bit vague and seem to only address your earlier (wrong) understanding of the question. – user643011 Aug 11 '21 at 08:15
-
Related question: Will `-fno-exceptions` compiler option trigger the same performance benefits as marking a function `noexcept` in this case: https://www.youtube.com/watch?v=AG_63_edgUg ? – user643011 Aug 11 '21 at 08:25
-
Update: https://stackoverflow.com/questions/10787766/when-should-i-really-use-noexcept#comment121480642_67134262 I tested how GCC and clang behave and that seems to support your statement. – user643011 Aug 11 '21 at 08:36