top | item 32447605

(no title)

petmon | 3 years ago

The C++ stdlib (aka STL) is 100% usable without exceptions. This is deliberate: major players and code bases disable exceptions, including Google, LLVM, Firefox, so it has to work and work well. And disabling exceptions is easy, simply pass `-fno-exceptions` to the compiler.

Fallible C++ functions do return an error code. errno is part of C++.

https://en.cppreference.com/w/cpp/error/errno

discuss

order

jcelerier|3 years ago

wouldn't say 100%, and -fno-exceptions does not disable exception propagation, it only prevents you from having the keywords "throw / try / catch" in your code. It's because it's not a C++ feature but a platform one which C++ leverages - when GCC compiles C code for instance it makes sure that their stack can be unwound in case there would be a callback implemented in C++ which would throw an exception (and because this is also a generally useful ability to have).

If you use the stdlib it will still throw (but disabling exceptions just means that now you can't catch it and it will be an automatic crash if it reaches main): https://gcc.godbolt.org/z/1s11f99a6 as the "throw" themselves are in the stdlib's implementation files, not in the headers so it isn't "your" code and isn't affected by -fno-exceptions.

programmer_dude|3 years ago

LOL, you haven't actually tried using C++ without exceptions have you?

benreesman|3 years ago

I have a pretty big C++ codebase that uses noexcept on almost everything. There are a few cases where deep in the call graph something has to have a try block, but most of the unavoidable exception semantics are around std::bad_alloc, and you’re in pretty deep at that point.

clang-tidy rarely if ever misses on noexcept violations these days.

Not sure what the LOL is about.

MrQuincle|3 years ago

Sure. Embedded programming.