3 ms·
The only pattern I can think of is to construct via a free function. The free function would call the real (basically empty) constructor, and then fill it in vi
by sseagull 11y ago
The only pattern I can think of is to construct via a free function. The free function would call the real (basically empty) constructor, and then fill it in via other member functions or by directly accessing the data (via friendship). This free function would be able to allocate memory, etc, and return error flags.
You could even make the real constructor private, which would prevent inadvertently creating an object with an invalid state.
(Could also be done with a static member function, I suppose)
- deleted 11y ago[deleted]
- gpderetta 11y agoYes, a free or static member function that returns an Either-like object is the way to maintain strong RAII invariants while avoiding exceptions. Either, named Result, has been proposed of the next standard. Unfortunately C++ still doesn't have good facilities for dealing with monads (i.e. do notation) making working with Either (and futures, and optionals) quite awkward.