How exactly can you use the standard C ++ library without exception? I use some vectors in my library. How to find out if the push_back function really succeeded? Is there a standard way to find out if an exception has occurred, or does it just exit (1) or something else?
You go to very restrictive territory as soon as you turn off exception handling in C ++.
Some standard library implementations, such as Dinkumware, allow you to disable exceptions. There's a macro definition issue, _HAS_EXCEPTIONS, as 0. STLPort has a similar convention with _STLP_USE_EXCEPTIONS = 0.
However, the standard definition of standard libraries does not exist if exceptions are disabled. Exception handling, for the most part, is largely rooted in C ++. Even dynamic_cast
and operator new/new[]
throw by default, and these are not library functions.
There is also no clear definition of what should happen even for standard library implementations that are not thrown away. If the push_back
sequence throws more memory for this sequence in the allocation process, what should happen? Is the item just not inserted? The standard interfaces of these sequences, of course, say nothing about when such an error occurs.
In addition, many C ++ libraries will generally use functions that are thrown as the new operator (and not the nothrow version). As a result, we move into many areas of undefined behavior after throwing exceptions.
I had to work for a company that forbade exception handling, because older programmers were premature optimizers who preferred C and thought that C ++ was horrible and inefficient (coincidentally they wrote some of the most inefficient codes in the team with a strong preference for linked lists as the default container, due to which the profiling hot spots were displayed on the left and right due to the fact that a huge number of small nodes were allocated / deallocated for everything, but this is different and thorium).
With embedded systems, the argument against exception handling can be a bit stronger, but without it it's hard to rely on C ++. I think the best thing you can do without exception handling is to take a crippled C ++ form without a lot of standard library parts that throw if you don't want to invest a lot of time trying to find workarounds and tricks specific to your a particular vendor of a standard library that may be more of a problem than it costs.