Boost logo

Boost :

Subject: Re: [boost] boost::optional<A,B,...>
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-03-04 04:17:41


On Wed, Mar 3, 2010 at 10:48 PM, 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.

I actually do just use variant directly:

boost::variant<unused,A,B>

Unused is used in spirit/phoenix/etc... and is completely empty, takes
no space, default constructable, etc... And if the id of the variant
tests 0, it is unused, so it is a simple bool test to that value.


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