Re: Initializing member references to dummy member variables
On Monday, 12 August 2013 20:17:28 UTC+1, =D6=F6 Tiib wrote:
On Monday, 12 August 2013 21:34:10 UTC+3, James Kanze wrote:
On Monday, 12 August 2013 16:59:59 UTC+1, =D6=F6 Tiib wrote:
On Monday, 12 August 2013 17:51:11 UTC+3, K. Frank wrote:
[...]
boost::optional is usually used to get rid of indirection when it
is not needed (indirection is mostly needed when the referred object
is big, hard to copy or polymorphic).
By far the most important use for things like boost::optional is
in return values from functions which may fail. (That's why it
has always been called Fallible in the past.)
If the function is a lookup, and returns something which is guaranteed
to exist beyond the function's lifetime, you return a pointer, but if=
the function has to synthesize the return value, there's nothing
with sufficient lifetime that it could point to. So you return
a Fallible.
Functions that may non-exceptionally fail and have to return some values
of a type that does not have "missing" or "null" states (like pointers).=
Yes. That's what the class was designed for originally. In
Barton and Nackman.
About the only other use is as a mutable member, for cached values.
I'd be very sceptical of any other use.
How about mutable lazily initialized members? Expensive to initialize
and not guaranteed that it is ever needed.
That's what I meant be cached values. Although it doesn't have
to be just initialization. If the value depends on other
members of the class, you can invalidate the cache each time one
of the other members changes, and recalculate it on an as needed
basis.
--
James