Boost logo

Boost :

Subject: Re: [boost] [optional] Safe optional
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2014-11-18 15:47:20


On Tuesday 18 November 2014 13:39:05 Felix Uhl wrote:
> Matt Calabrese wrote:
>
>
>
> > I know that there are others (Eric?) who prefer to think of optional as a
> > container of size 0 or 1, so it could also make sense to provide an
> > accessor to a range. One interesting effect of this is that now
> > range-based-for sort of becomes a "visit if." There are probably other...
> > interesting uses of standard algorithms.
>
>
>
> I don’t know why, but I really like that idea;
>
>
> int f(optional<int> input){
>
> int ret = 0;
>
> for(int opt : input){
>
> ret = opt;
>
> }
>
> return ret;
>
> }

Looks weird to me. It will look unintuitive to newcomers, IMHO. I remember it
was always strange to me that to clear an optional I had to assign none
instead of calling clear(). This is the same kind of thing.

> It somehow seems like a really interesting Syntax, but to be honest,
>
> it doesn’t really offer any benefits, does it?
>
> You neither have any improvements on performance, nor any on
>
> code clarity.

The performance can be worse than a traditional if. The for statement requires
iterator increment, comparison and a conditional jump. That is in addition to
the test if the optional is empty and returning pseudo-iterators. Compilers
may optimize this overhead, though.


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