Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2003-10-07 00:52:47


E. Gladyshev wrote:

> --- Eric Friedman <ebf_at_[hidden]> wrote:
> [...]
>
>>However, as I noted in previous messages, allocation on the heap can be
>>avoided entirely for any type Ti in variant<T1, ..., TN> if T1 is nothrow
>>default-constructible. I would suggest then that, if none of your types are
>>guaranteed to have a nothrow default constructor, you work with
>>variant<boost::empty, T1, ..., TN>. Even if you never make direct use of
>>boost::empty, this will ensure all allocation will be via placement new and
>>never on the heap.
>
> What was the reason for giving the first type such a special meaning?
> I don't see why the first type should be any more special than others.
> I think it is quite confusing.

To check each type Ti in variant<T1,...,TN> would add O(n) compile-time
complexity. To check only the first type T1 adds only O(1) complexity.

In short, then, there's no theoretical reason to treat T1 specially, but
it seemed to me a good compromise. Indeed, the optimization is usable
even in generic contexts, e.g.:

   template <typename U1, ..., typename UN>
   class my_class
   {
     variant< boost::empty, U1, ..., UN > var_;
   };

or if using a MPL-compatible typesequence:

   template <typename Types>
   class my_class
   {
     variant<
         typename mpl::push_front< Types, boost::empty >::type
> var_;
   };

- Eric


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