开发者

Will turning off exceptions in C++ lead to undefined behavior according to the current standard?

I often hear the C++ exception system can be disabled as you should not pay for what you do not 开发者_JAVA百科use. If I choose to compile my C++ program without exceptions will it result in undefined behavior?


The current (and future) C++ standard has no notion of turning off exceptions. So technically yes, doing so leads to undefined behavior, if you ask the language lawyers. Realistically implementations try to define reasonable behavior for this popular extension. Consult your documentation.


To add to Howard's answer.

The general issue is that code written with exceptions in mind could suddenly become bug-riddled if you turn them off.

A simple call to new for example, is supposed to throw the std::bad_alloc exception if the memory request cannot be honored. In case you turn off exceptions it'll return 0 instead: are you sure that all calls to new check that the return is not 0 ? Even those in the standard library or 3rd party libraries ?

CLang recently introduced a diagnosis that prevents compilation if throw or try are used when compiling with exceptions disabled, because it means that the code has not been properly prepared, but I am unsure wrt the other compilers, so watch out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜