Holding char arrays in boost variants, what is the correct way?

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; // 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? Thanks, Michael Goldshteyn

2011/6/14 Michael Goldshteyn <mgoldshteyn@comcast.net>
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;
[snip]
Thanks,
Michael Goldshteyn
Hi, I don't have an answer to your question, but I have two aside questions: 1) Why didn't you try variant<int,char[MAX_LEN]> in the first place? (I don't know if it should work or not) 2) Why not variant<int,vector<char> >? Regards, Kris

"Krzysztof Czainski" wrote in message news:BANLkTi=5=Cww4Y9zT1WGQj4Tgzb9tGAd8g@mail.gmail.com...
Hi, I don't have an answer to your question, but I have two aside questions: 1) Why didn't you try variant<int,char[MAX_LEN]> in the first place? (I don't know if it should work or not) 2) Why not variant<int,vector<char> >?
1) doesn't work 2) is an idea, but I was hoping to avoid dynamic allocation in cases where I know the max size Michael Goldshteyn

2011/6/14 Michael Goldshteyn <mgoldshteyn@comcast.net>
"Krzysztof Czainski" wrote in message news:BANLkTi=5=Cww4Y9zT1WGQj4Tgzb9tGAd8g@mail.gmail.com...
Hi, I don't have an answer to your question, but I have two aside questions: 1) Why didn't you try variant<int,char[MAX_LEN]> in the first place? (I don't know if it should work or not) 2) Why not variant<int,vector<char> >?
1) doesn't work 2) is an idea, but I was hoping to avoid dynamic allocation in cases where I know the max size
Michael Goldshteyn
Have you looked at boost::array?
Kris

On 14/06/2011 17:23, Michael Goldshteyn 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;
// This does, but seems like a lot of work template <int len> struct MyChar { char val_[len]; };
Use boost::array (or std::array if you have it).

On Tue, Jun 14, 2011 at 8:23 AM, Michael Goldshteyn <mgoldshteyn@comcast.net
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?
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. Troy

On 14/06/2011 18:18, Troy Straszheim wrote:
On Tue, Jun 14, 2011 at 8:23 AM, Michael Goldshteyn<mgoldshteyn@comcast.net
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*.
That's not the problem; the problem is that boost::variant requires that all types be CopyConstructible.
participants (4)
-
Krzysztof Czainski
-
Mathias Gaunard
-
Michael Goldshteyn
-
Troy Straszheim