Boost logo

Boost :

Subject: Re: [boost] Holding char arrays in boost variants, what is the correct way?
From: Troy Straszheim (straszheim_at_[hidden])
Date: 2011-06-14 12:18:24

On Tue, Jun 14, 2011 at 8:23 AM, Michael Goldshteyn <mgoldshteyn_at_[hidden]
> wrote:

> What is the correct way of getting several character arrays into a variant:
> // This doesn't work
> typedef boost::variant<int,char[256],char[8192]> MyType;

And it is odd looking. IIRC the problem is that those two array types decay
to char*.

// This does, but seems like a lot of work

> template <int len>
> struct MyChar
> {
> char val_[len];
> };
> typedef boost::variant<int,MyChar<256>,MyChar<8192> > MyType;
> What is the best way to do this?


   variant<int, boost::array<char, 256>, boost::array<char, 8192> >

is what you've got above minus some wheel-reinventing, but IIRC the variant
always carries enough storage around with it to store the largest type in
its argument list (see make_storage)... so you won't save space with this,
if that is your intention.


Boost list run by bdawes at, gregod at, cpdaniel at, john at