Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2002-12-11 09:31:17


----- Original Message -----
From: "Tanton Gibbs" <thgibbs_at_[hidden]>
To: "Boost mailing list" <boost_at_[hidden]>
Sent: Tuesday, December 10, 2002 10:10 PM
Subject: Re: [boost] Formal review: Optional library

> > > > That's why it hands you a pointer to the value, because you can test
> the
> > > > pointer
> > > > for NULL; but that's it.
> > > > IOWs, the pointer returned by optional is conceptually a handle, not
a
> > > > pointer.
> > > > That's why I like the member function peek() much better than get().
> > >
> > > I still much prefer the standard get(), even if I bought into the rest
> of
> > > the rationale. It's what people expect because of std::auto_ptr.
> > >
> > I see.
> > I counted 2(two) in favor of get(), and one (me :-) in favor of peek().
> > I'll be democratic on this one, though I hope I can get more counts.
>
> For what its worth, I vote in favor of value semantics. It seems much
> easier
> to me to remember to call initialized when I want to test validity than it
> does
> to remember which of *, peek, get, get_value to use in which case. Also,
> the fact that I can assign a non pointer value to a smart-pointer is kinda
> strange.
> To me, it just doesn't fit the smart pointer mold and the interface is
> confusing
> because it is trying to shape an elephant into a rhino -- yeah, their
close,
> but
> shaping a hippo to a rhino might be easier. Therefore, I urge you to
> reconsider
> value semantics as I think those examples were much cleaner and clearer
than
> the examples I have been seeing.
>
> Thanks,
> Tanton
>
value semantics were not just considered but used.
The original optional<> I implemented used value semantics,
but after a long time, I found it a lot more confusing that
with value semantics.
Pointers convey the notion of uninitialization quite naturally.
For instance, for an optional argument to a function
pointer are typically used.
Consider:

void foo ( int* opt )
{
  if ( opt )
  {
    bar(*opt);
  }
}
foo(NULL);

this is the typical way you deal with an optional argument.
optional<> follows the same pattern for those cases were true
pointers cannot be used since it would require a dynamic
allocation.

optional<int> foo()
{
  return optional<int>(3);
}
void gee()
{
  optional<int> opt = foo();
  if ( opt ) // pretend safe-bool is added
    bar(*opt);
}

There is an analogy between the way pointers are ordinarily used
for optional function arguments and optional<>. The analogy
should help users become familiar with optional<>.

Fernando Cacciola


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