Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-10-06 17:43:12


--- "David B. Held" <dheld_at_[hidden]> wrote:

> >
> > struct my_type
> > {
> > ...
> > };
> >
> > typedef boost::variant< my_type > my_v;
> > std::vector< my_v, my_allocator<my_v> > v;
> >
> > f()
> > {
> > my_type t;
> > v.push_back( t );
> > }
> >
> > How does the my_type variable gets allocated when it is
> > transferred to the variant?
>
> If variant doesn't use dynamic allocation, I assume that means
> that the my_types are stored directly in v, but wrapped in the
> variant. So when push_back() is called, if it needs to resize
> the vector, it asks the allocator for a larger chunk of my_v's.
> If not, it just copies into the existing reserved space. At
> some point, a variant would get c'ted from t, and copied
> into that array. No per-variant heap allocation required.

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!

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?

Eugene

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com


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