Your connect method can still throw std::bad_alloc.
Re:Nanomsg postmortem and other stories
by IshKebab on Friday February 12, 2016 @01:03PM
(#11089065)
You could easily catch that within the method though. But in general I think throwing an exception on memory allocation failure is much better than the alternative, which is - in practice - assuming it succeeds and segfaulting.
Re:Nanomsg postmortem and other stories
by kinofcain on Monday February 08, 2016 @10:20AM
(#11058542)
Boost has variant, which is also a discriminated Union though the visitor pattern ergonomics aren't quite as nice as result in rust or doing the same thing with enums in swift. There's work to get something like variant into the standard.
1. Exceptions are awkward because they separate the error handling from the context that is usually required to correctly handle an error (i.e. where it occured).
2. Exceptions are the only reasonable way for a constructor to fail. You can't return error codes from a constructor.
I actually agree with the point about exceptions, and I'm glad Rust didn't use them. That said, the solution is trivial (and the author admits it) - don't use exceptions.
The second point is also actually a fairly good one, and again Rust has fixed this - it uses static `new()` initialisers that create object (or not, if they fail). Fortunately we can actually copy this method in C++. Here's an example for a network connection object:
Unfortunately as far as I can see there is no Rust-like `result<>` type in Boost or the standard library yet, but it would be fairly easy to write your own.It seems pretty crazy to give up C++ for these fixable flaws. Especially given how many unfixable flaws C has.