4 ms·
On the other hand, constructors don’t allow returning an error because C++ has exceptions.
by masklinn 4y ago
On the other hand, constructors don’t allow returning an error because C++ has exceptions.
- deleted 4y ago[deleted]
- pjc50 4y agoI mean .. yes, but easy multiple-value-return unpacking is also a fairly new feature. Back in the 90s you had one (1) lvalue. Making the "return (thing, error)" pattern miserable to use.
- masklinn 4y agoI was going to say that such an alternative history C++ could have had MRVs earlier, or even sum types... but apparently exceptions were added a decade after constructors (if we draw that back to the original "C with classes"). So yeah, you're right that constructors led to exceptions, unless the designers of C++ had been willing to go back to the drawing board and entirely redo constructors.
- chrisseaton 4y ago> constructors don’t allow returning an error because C++ has exceptions Didn't constructors (1979) come before exceptions (1986)? So no that's not the case.
- masklinn 4y agoYes, I didn't originally know that (I thought constructors and exceptions would have been co-implemented and didn't consider that faillible ctors would have been outright impossible for a decade), and found out after pjc50's reply: https://news.ycombinator.com/item?id=33572348 https://news.ycombinator.com/item?id=33572348
- chrisseaton 4y agoPeople used to use a flag in objects to say 'invalid because constructor failed', or make the constructor infallible, and have a separated initialise method which could return an error code.