Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2012-02-09 06:20:41


> optional< T& > is a useful thing when you want to apply operators (such
as
> relation operators or streaming) to the referred value. In generic code
you
> don't have to specialize for pointers to do the right thing. I'm going to
use
> this property in my Boost.Log library.

Andrey, would you mind giving us a short example of how you want to use an
optional reference? I am in the middle of designing the new optional
interface for TR2, and came to the conclusion that in order to avoid
counter-intuitive semantics for optional reference assignment, I had better
remove it at all; that is, optional references are to be limited: they
should provide no assignment. You could still use optional values and
optional references in generic code but with reduced interface:

template <typename T> // T is a ref or a value
void use( std::tr2::optional<T> opt, T nval )
{
  if (opt) {
    std::cout << *out; // fine
   *opt = nval; // fine, assigning T's not optionals
    opt = nval; // invalid if T is a ref
    opt = opt; // invalid if T is a ref
  }

  if (needToRebindAReference()) {
    opt.emplace(nval); // valid - always rebinds
  }
};

Would such a limited interface as I described above be enough for your
generic usage of optional?
Regards,
&rzej


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