Throwing Exceptions from Coroutines
ibob.bg
ibob.bg
myobj obj;
obj.complete_initialization();
Do they? How about the constructor-that’s-not-a-constructor pattern: myobj::create(…);
And have that return an option or result type? This is the normal way to do this in several languages (Rust, but also plenty of functional languages), and C++ can do it too, awkwardly, using private constructors and public static helpers. (Awkward because the object may still need to exist, temporarily, in a partially constructed state outside a constructor.)I wonder whether coroutines in C++ could pull this off without being too tedious.
If you are trying to handle OOM, then you should at least have a big fuzz test setup that causes malloc to spuriously return null, and also set your debug builds to do this too.
Good luck writing stable software that way (vs just not dynamically allocating stuff on the heap) —- everything becomes exponentially more complicated and difficult to reason about / test.
class UTF8String {
public:
static optional<UTF8String> FromBytes(const void *buffer, size_t len);
private:
UTF8String(...);
void *bytes;
size_t length;
};
UTF8String::FromBytes can fail if the input is invalid. In this particular case, it can obviously be implemented without constructing a UTF8String until it's past the point of no return. But I can imagine cases where pulling this off is awkward.Though not common, this factory function method does not work for immovable types. You can't yoink these types out of the optional<T> that's returned and put them in a vector<T>, for instance.
I had to scroll to the end of the post and see the C++ tag before this phrase made sense...
FTFY. Now it applies to all languages and not just C++.
Anyway, I’ve been happily using -fno-exceptions for over a decade.