
On Wed, Mar 3, 2010 at 10:48 PM, Daniel Larimer <dlarimer@gmail.com> 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.