Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2002-11-22 16:47:33


"Rozental, Gennadiy" <gennadiy.rozental_at_[hidden]> wrote in message
news:1373D6342FA1D4119A5100E029437F6401169E7F_at_clifford.devo.ilx.com...
> > > We already talked about this: pointer will add extra memory access,
> > optional
> > > should not (in fact it should be inlined and won't be
> > different from by
> > > value parameter)
> > >
> > You are mis-remembering our previous talk.
> ....
>
> No. I do remembr that we agreed that pointer semantics is better. I still
> agree with that. Nevertheless my point above was following:
>
> Even if optional<T> has pointer semantics, unlike pointers *optional<T>
> won't produce extra memory access.
>
OK. Now I understand what you meant, sorry.
You are right, the dereference from an optional<T> parameter won't compile
as an extra indirection as will be the case with a true pointer. A good
point if performance is important at this level.

>
> > > implicit conversion to optional should not be dangerous anyway.
> > >
> > I disagree.
> > Implicit conversions are usually problematic, but with
> > optional<>, it is
> > even worst, since
> > (1) a conversion from an uninitialized optional is undefined
> -----------------------------^
> we talked about "to" conversion
>
> > (2) the distinction between
> > (a) the operation of testing whether an optional is
> > initialized or not
> > (b) the operation of accesing the optional value (in
> > this case via a
> > conversion)
>
> we talked about "to" conversion
>
OK, I missed your point again. Two sorries :-))

Your are asking why can't the constructor be not explicit, right?

Well, this would allow the 'direct' syntax fn(1,3) that Vincent wanted,
but...

It would entirely break the pointer semantics because the following would be
allowed:

void foo()
{
   optional<int> opt ;
   opt = 3 ; // This will construct a temporary optional<int>(3)
   if ( opt == 0 ) // tmp optional<int>(0) here.
}

> > Maybe, but actually, I don't think optional<> should work
> > with references.
> > It is supposed to wrap a 'value', not a reference/pointer.
> >
> > Fernando Cacciola
>
> Why? I always did not like the fact that I need to switch to pointers when
> my reference argument became optional.
>
Wait... maybe I keep misundestanding you, but...

Even if it would be allowed to have optional<int&>, you would still have to
use it as if it were a pointer:

void foo ( optional<int&> p = optional<int&>() )
{
  if ( !!p ) // or ( peek(p) ) or ( initialized(p) )
  {
     int& the_p = *p ;
     the_p = 3 ;
     etc...
  }
}

So you still gain little, from foo() perspective, by using optional<> since
you can write it almost exactly the same as:

void foo ( int* p = NULL )
{
  if ( !!p )
  {
     int& the_p = *p ;
     the_p = 3 ;
     etc...
  }
}

--
Fernando Cacciola

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