0

Calling std::terminate() from code compiled with disabled exceptions, I noted that behavior differs for gcc and msvc. In first case program was aborted as I expected, whereas in second case nothing happened (the program continued execution).

As it turns out, msvc has dummy implementation for terminate function when _HAS_EXCEPTIONS macro is not set:

inline void __CRTDECL terminate() _NOEXCEPT
{   // handle exception termination
}

Visual Studio 2017, Version 15.7.2

cppreference says:

std::terminate() may also be called directly from the program.

Doesn't this mean that abort/handler must be called anyway, not depending on exceptions ability?

KernelPanic
  • 101
  • 2
  • 7
  • While it is certainly a good idea not to make too many gratuitious changes in any not-C++ language to the base language, one cannot appeal to the C++ standard as it is not C++, only to common sense, which might not be as common as hoped for, or simply not ours. – Deduplicator Jun 30 '18 at 12:34
  • I'm not sure how you are getting this behavior. If exceptions are disabled than std::terminate invokes `abort`. I don't see any kind of dummy implementation being actually called. [Probably related](https://stackoverflow.com/questions/20221715/has-exceptions-1-vs-has-exceptions-0) – user7860670 Jun 30 '18 at 13:20
  • 2
    _"The option _HAS_EXCEPTIONS=0 is undocumented, untested, and unsupported by Microsoft. ..."_: source https://blogs.msdn.microsoft.com/vcblog/2015/07/14/stl-fixes-in-vs-2015-part-2/ – Richard Critten Jun 30 '18 at 13:21
  • @RichardCritten, I think this comment should be the answer. – KernelPanic Jun 30 '18 at 14:27
  • @VTT, do you have different implementation in the exception header? What version of Visual Studio do you have? – KernelPanic Jun 30 '18 at 14:32
  • I've just disabled exceptions. Note that I did not mess with `_HAS_EXCEPTIONS`. – user7860670 Jun 30 '18 at 14:34
  • Re: "Doesn't this mean..." -- it means that you have to read the documentation for the compilers that you're dealing with. Turning off exceptions is not part of the C++ standard, so anything that the standard says is irrelevant when you do this. – Pete Becker Jun 30 '18 at 14:37
  • It seems that _HAS_EXCEPTIONS=0 is only way to turn-off exceptions in STL. [Related](https://stackoverflow.com/q/553103/9027283) – KernelPanic Jul 03 '18 at 10:06
  • @PeteBecker: Compiler specifics shouldn't be in conflict with the standard, do they? – KernelPanic Jul 03 '18 at 10:38
  • @KernelPanic -- the C++ standard isn't a law. Compiler vendors don't **have to** follow it. There's commercial pressure to do so, but compilers have lots of non-conforming "features", like turning off exceptions. – Pete Becker Jul 03 '18 at 12:34

0 Answers0