Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2003-10-06 18:06:07


E. Gladyshev wrote:
[snip]
> Interesting, does it mean that when
> you do my_v *p = new my_v;
> it actually allocates space enough to hold
> the largest data type in the variant?
> Does it mean that sizeof(my_v) >= largest_type_in_my_v?
> I guess 'variant::make_storage' takes care of that,
> and 'variant' uses in-place new/delete all the way after.
> If I am right(?), it is very cool!

Yes, you're right.

> There is the following line in the variant.hpp
> // Backup lhs content...
> LhsT* backup_lhs_ptr = new LhsT(lhs_content);
>
> Seems troubling to me.
> I thought variant never does direct
> memory allocations, does it?

Please see http://aspn.activestate.com/ASPN/Mail/Message/boost/1826694
describing the considerations that went into this.

Note, too, that this code will never execute for any T that is nothrow
copyable or if the first bounded type of the variant is nothrow
default-constructible (e.g., variant<int, ...> or variant<boost::empty,
...>, etc.).

Eric


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