Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-08-30 13:48:41


----- Original Message -----
From: Douglas Gregor <gregod_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, August 30, 2001 3:16 PM
Subject: Re: [boost] Re: optional vs variant vs any

> On Thursday 30 August 2001 01:14, you wrote:
> > In the case of optional, it makes complete sense to provide only a
default
> > constructor, because if you could initialize x with something you
shouldn't
> > be using optional in the first place; that is, it is completely
senseless
> > to write optional<T> x = T(...);
>
> I disagree with this.
>
> optional<T> foo() {
> if (everything_is_happy_in_the_world) {
> return optional<T>(some_value);
> }
> else {
> return optional<T>();
> }
> }
>
Hmm. Good point again.
Still, I think this shouldn't be the proper usage. I would write the above
like this:

optional<T> foo() {

   optional<T> result ;

   if (everything_is_happy_in_the_world) {
     *result =some_value;
   }

   return result ;
}

Supporting direct initialization has the drawback that both statements below
become equivalent, which I find confusing:

  optional<int> v ;

  v = 3 ;
  *v = 4 ;

unless the constructor is made explicit, but even then I find it confusing:

  optional<int> v ( 3 ) ;
  *v = 4 ;

I might be happy with a third alternative, a named constructor:

  optional<int> v = optional<int>::create_initialized(3) ;
  *v = 4 ;

I still find odd to use it like you used it. I prefer no direct
initialization.
But if there is general agreement to support direct initialization I can add
it (but only explicit or with a named constructor)

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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