by cpgxiii · 4y ago · 0 comments
Don't want to use exceptions? Keep the constructors private/protected, provide factory functions that return a Result<Type, Condition> (in whatever value/pointer forms you need) and keep all the benefits of RAII and enforced invariants without requiring your users to catch exceptions.
Fallible C++ functions do return an error code. errno is part of C++.
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.
clang-tidy rarely if ever misses on noexcept violations these days.
Not sure what the LOL is about.
Some flags i use to disable exceptions
# The default flags will be used in all build types
# The C/C++ shared flags are in DEFAULT_C_AND_CXX_FLAGS, the ones particular for C or C++ in DEFAULT_X_FLAGS
# The flags mean the following:
# * no-exceptions disable exceptions support and use C++ libs without exceptions
# * delete-dead-exceptions throw away instructions which only purpose is to throw exceptions
# * no-unwind-tables stack-walking code for backtrace generation (TODO: we might want to enable this in Debug!)
# * no-non-call-exceptions do not convert trapping instructions (SIGSEGV, etc) into exceptions
# * thumb use Thumb mode (optimized instruction set)
# * function-sections put every function in its own segment, now it can be garbage collected
# * data-sections put every variable in its own segment
# * no-strict-aliasing allow *int = *float aberrations
# * no-builtin do not use abs, strcpy, and other built-in functions
# * short-enums use a single byte for an enum if possible
SET(DEFAULT_CXX_FLAGS "-std=c++17 -fno-exceptions -fdelete-dead-exceptions -fno-unwind-tables -fno-non-call-exceptions")If he wants to use that init/term pattern, he can just not use constructors/destructors and do it all in init/term (not suggesting that he should). What, you have to add a "state machine" to your objects because other developers on the project will willfully ignore the project's policies?
What about the C version? It also has a half-initialized state between the declaration and `foo_init`. Except here you can't use defensive programming against it and accessing the half-initialized state is simply UB because it's in fact fully uninitialized. Or maybe half-initialized or initialized wrong if the developer hand-initializes some fields.