Boost logo

Boost :

Subject: Re: [boost] [review] Review of Outcome v2 (Fri-19-Jan to Sun-28-Jan, 2018)
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2018-02-01 04:24:38


On 1/02/2018 15:38, Emil Dotchevski wrote:
>> My question still stands though. If you don't use exceptions, in C++, how
>> do you protect users from calling member functions on objects that have not
>> been initialized?
>
> I mean haven't been initialized or failed to initialize.

The usual technique to manage that is to separate no-fail construction
(constructor) from fail-possible initialisation (an init() method).

Sometimes this requires weaker invariants than otherwise (eg. allowing
an empty state).

Often this two-phase construction is hidden from consumers by making
both of them private and publishing a static factory method instead.

Note this is also the same technique commonly employed by people who
want to guarantee use of shared_ptr (for use with shared_from_this
internally).

The factory method technique also allows somewhat restoring a stronger
invariant -- only the constructor and destructor need to cope with empty
or otherwise uninitialised instances; other methods can be reasonably
assured that no such instances escaped the factory method.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk