'Writing C++ without exceptions?
This answer mentions two ways to handle a C++ library (such as Qt) which is not exception safe:
- Isolate it in exception-safe wrappers
- Give up exceptions and adapt to its style
The answer goes into detail about the first option, but what are the consequences of the second option: giving up exceptions?
When writing C++ without exceptions, how is the use of the language restricted? For example, are there parts of the standard library which are not safe to use?
(In particular, I assume that in the case of std::bad_alloc, I would have no choice but for my program to exit?)
Solution 1:[1]
It's not hard. In fact I argue that the Result<T>
style (also known as expected<T>
in Andrei's talk, but I was inspired by Rust's version, particularly the try!
macro, except that I don't template my Err
variant (for functions that fail, all errors are the same)) is a lot easier than using exceptions.
The one thing you really have to give up is constructors that do something that can possibly fail. Instead, use the named constructor idiom and only use pretty-much-aggregate constructors after you've already done.
As far as bad_alloc
, just let it kill the process by unwinding.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martin Valgur |