Boost logo

Boost :

Subject: Re: [boost] [optional] Safe optional
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2014-11-17 05:12:40


On Sun, 16 Nov 2014 23:04:43 -0800, Andrzej Krzemienski
<akrzemi1_at_[hidden]> wrote:

> Hi Everyone,
> I would like to run an idea through everyone in this list. There is a
> recurring complaint about Boost.Optional that it allows you to do
> "unsafe"
> things:
> 1. Inadvertent mixed comparisons between T and optional<T>
> 2. Unintended conversion from T to optional<T>

The problem with optional is that it tries to be a drop-in replacement
proxy for its underlying type, and, unfortunately, it can't fully do that.
So it ends up with an identity crisis. IMO, optional should be treated as
a first class object, not a thin wrapper around T, that means no implicit
conversions to/from T, no implicit comparisons with T, etc... Last time I
looked at this, that will solve the reference rebinding gotcha. That is,
you'll have one behavior for optional<T> and optional<T&>. It will also
solve the following gotcha too:

   optional<bool> b(false);

   if(b == true)
   {
      // Does not get executed.
      std::cout << "???\n";
   }

   if(b)
   {
     // Gets executed.
     std::cout << "!!!\n";
   }

which I believe to be an ever bigger problem.

In short, and IMHO optional should be a true model of the pointer concept,
like iterators and shared_ptr.


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