top | item 27733644

(no title)

correct_horse | 4 years ago

I haven't found a good solution to a fallible constructor without exceptions. You might want this if you have a C++ wrapper around a file or some other OS primitive. I would love some allowance for constructors returning std::optional. One problem with this is that child classes' constructors would have to return optional too or else they might throw an exception when they call optional::value.

discuss

order

varajelle|4 years ago

Don't write faillible constructor. Instead, you can write a static method that returns a std::optional, for example.

jcelerier|4 years ago

That does not work for data members, unless you are willing to have the entirety of your software wrapped and unwrapped in optionals up to main, which looks pretty much like what people had in the 70s and thought : "okay, maybe there is a language feature we could have to abstract that repetitive mess"