3 ms·
Agreed, but how certain can you be that the program generates no exceptions? Simply instantiating a pointer to class using new uses exceptions. Without except
by lelanthran 4y ago
Agreed, but how certain can you be that the program generates no exceptions?
Simply instantiating a pointer to class using new uses exceptions.
Without exceptions all constructors that fail turn into undefined behaviour. The caller will never know that the instance they are using is filled with random bytes.
- ridiculous_fish 4y agoI'm not sure that a failing constructor is UB under -fno-execptions. The libstdc++ manual suggests that these will instead abort: https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html#intro.using.exception.no https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_except...
- zabzonk 4y ago> I'm not sure that a failing constructor is UB under -fno-execptions the c++ standard does not allow for disabling exceptions, so at best this would be implementation-dependent behaviour, at worst UB. in other words, using -fno-execptions, and similar on other compilers, raises compatibility problems.
- mort96 4y agoYou're right, the C++ standard does not allow for disabling exceptions. So C++ with exceptions disabled is not a language described by the standard. We instead have to look at the documentation of this "C++ with exceptions disabled" language. And this "C++ with exceptions disabled" language defines that exceptions terminate the program. There is no UB anywhere here, everything is well-defined. You're right again though that there are compatibility problems, you could hypothetically use a compiler whose '-fno-exceptions' option actually defines throwing exceptions to be UB. No compiler would do that since it's obviously insane, but it is a possible alternate "C++ with exceptions disabled" language. Make sure to not use such compilers. And it's probably a good idea to keep your code valid C++ anyways, meaning a standard C++ compiler will terminate the program due to an uncaught exception while GCC and Clang with -fno-exceptions will terminate the program by calling abort(). This way, GCC and Clang's -fno-exceptions behavior is just an optimization for when compiling on those compilers.
- zabzonk 4y ago> "C++ with exceptions disabled" language not a language standard i was previously aware of.
- mort96 4y agoCorrect! It's a language defined ad-hoc by GCC and Clang. GCC's man page describes its behavior with '-fno-exceptions' to be essentially: act like a normal C++ compiler, except produce code which calls abort() where it would otherwise have engaged the exception throwing machinery. That defines a kind of "C++ with exceptions disabled" language, but it's not a standard.
- kelnos 4y ago> And this "C++ with exceptions disabled" language defines that exceptions terminate the program. Except that's not a language that is standardized, so there's nothing that defines anything here. GCC and Clang may call std::terminate() when exceptions are disabled and some library/dependency throws, but unless your build process aborts when a compiler other than GCC or Clang is used, you can't rely on that behavior in general.
- mort96 4y agoAs I wrote in response to your sibling comment which pointed out exactly the same thing: Correct! It's a language defined ad-hoc by GCC and Clang. GCC's man page describes its behavior with '-fno-exceptions' to be essentially: act like a normal C++ compiler, except produce code which calls abort() where it would otherwise have engaged the exception throwing machinery. That defines a kind of "C++ with exceptions disabled" language, but it's not a standard.
- pjmlp 4y agoUnless there is std::nothrow in every new invocation.