Boost logo

Boost :

Subject: Re: [boost] boost::optional<A,B,...>
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2010-03-04 01:08:59


On 4 March 2010 00:48, Daniel Larimer <dlarimer_at_[hidden]> wrote:
> I have a situation where I have a class that stores two mutually exclusive optional values.  This results in double overhead of two bools and twice the "reserved" space.   I could opt for a boost::optional<boost::variant<A,B> >, but this would incur extra costs associated with the implementation of boost::variant.   Considering the fact that boost::variant<> explicitly guarantees to be always "valid", it seems like it would be reasonable to have boost::optional<A,...> that behaves like boost::variant<>, yet is optimized specifically for allowing "empty" states as well.
>
> The boolean could then become the type index where 0 is empty, 1 is A, 2 is B etc.
>

It seems like boost::variant is already 99% of the way there.

"So as to make the behavior of variant more predictable in the
aftermath of an exception, the current implementation prefers to
default-construct boost::blank if specified as a bounded type instead
of other nothrow default-constructible bounded types."
~ http://www.boost.org/doc/libs/1_42_0/doc/html/variant/design.html#variant.design.never-empty.optimizations

With that, I think you might get almost exactly what you want just by
changing v.empty() to be v.operator==(boost::blank()) instead of
always false and adding a few convenience functions.


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