Boost logo

Boost :

Subject: Re: [boost] [optional] std::tr2::optional
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2011-11-19 06:18:13


> This is more of an addition to your list, but I think adding

> the | operator for getting the optional value or a default value would be
nice.
> For example:

> optional<double> safe_sqrt(double x)
> {
> if (x < 0) return none;
> else return sqrt(x);
> }

> double x = safe_sqrt(4) | 0; //x = 4
> double y = safe_sqrt(-1) | 0; //y = 0
> I dont know if this is possible for tr2, but would be really nice
> addition in the future. Especially, since a lot of times i just
> want a default value if the optional value doesn't exist.

Note, that boost::optional already provides a functionality for that,
although with a slightly more verbose syntax:

double x = safe_sqrt(4).get_value_or(0);

std::tr2::optional would also provide this functionality.
If this is only about shorthand notation, this item would be controversial.
bitwise-or is a better choice that logical-or but it might turn out too
confusing and disturbing for programmers that do not want to learn what
overloaded operators mean for every type they use. What I mean to say is
that liberal use of overloaded operators is a general controversy.

Regards,
&rzej


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