468,780 Members | 2,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

Initializing a struct with an array literal

Hi,

I am trying to test a command line parser. So I came up
with this (simplified):

struct test {
int argc;
const char** argv;

template< int N >
test(const char* (&a)[N])
: argc(N-1)
, argv(a)
{
}
};

const char* a1[] = { "1", "22", "333" };
const char* a2[] = { "1", "22", "333" };

const test v1[] = { test(a1)
, test(a2) };

const test v2[] = { test( { "1", "22", "333" } )
, test( { "1", "22", "333" } ) };

template<int N>
void testParser(const test (&arr)[N]);

int main()
{
testParser(v1);
testParser(v2);
return 0;
}

Both VC7.1 and Comeau choke on 'v2'. (Comeau says
"expected an expression", VC issues a "missing ')'
before '{'" -- not very helpful.)
Obviously I can't initialize the struct with an
array literal. I can, however, initialize it with
an array object. The reason behind this escapes
me.
What's more important, I would really like to have
my test cases in one line and not split into two
objects.

Ideas anyone?

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett

Nov 17 '05 #1
7 1404
Hendrik Schober wrote:
Hi,

I am trying to test a command line parser. So I came up
with this (simplified):

struct test {
int argc;
const char** argv;

template< int N >
test(const char* (&a)[N])
: argc(N-1)
, argv(a)
{
}
};

const char* a1[] = { "1", "22", "333" };
const char* a2[] = { "1", "22", "333" };

const test v1[] = { test(a1)
, test(a2) };

const test v2[] = { test( { "1", "22", "333" } )
, test( { "1", "22", "333" } ) };

Both VC7.1 and Comeau choke on 'v2'. (Comeau says
"expected an expression", VC issues a "missing ')'
before '{'" -- not very helpful.)
Obviously I can't initialize the struct with an
array literal.


C++ doesn't have an "array literal." What it has is special syntax for
array initialization. You can't just make an array anyplace you want
with the array inititialization syntax.

-- Andrew Bell
an************@gmail.com

Nov 17 '05 #2
"Hendrik Schober" wrote:
Hi,

I am trying to test a command line parser. So I came up
with this (simplified):


Because there are no array literals in C++ (like Andrew said), you are going
to have to simulate it. Play with this code a bit to see if it can do what
you want:

template < int N >struct test {

int argc;
CString argv[N];

test& operator,(const char* a){argv[++argc] = a; return *this;}

test():argc(0){}
test(const test<N>& a){for(int j = 0; j < N; ++j)argv[j] = a.argv[j];}
test(const char* (&a)[N]){for(int j = 0; j < N; ++j)argv[j] = a[j];}
};

typedef test<3> mytest;
const char* a1[] = { "1", "22", "333" };
const char* a2[] = { "1", "22", "333" };

const mytest v1[] = { mytest(a1)
, mytest(a2) };

const mytest v2[] = { mytest( (mytest(), "1", "22", "333" ) )
, mytest( (mytest(), "1", "22", "333" ) ) };

template<int N>
void testParser(const mytest (&arr)[N]);

int mainx()
{
testParser(v1);
testParser(v2);
return 0;
}

Cheers,
Mike
Nov 17 '05 #3
mike <mi**@discussions.microsoft.com> wrote:
[...]
Because there are no array literals in C++ (like Andrew said) [...]
Andrew, Mike,

thanks for the explanation!
Too bad I haven't found a way to do this. I hoped I
could somehow easily put all the data for one test
into one line. But I think in the end I will have
to live with what I got...
[...] Play with this code a bit to see if it can do what
you want:
[...]
Thanks. There's two things I don't like about it:
1. Overloading the comma operator for some simple
test app seems a bit over the top to me. :)
2. I do need the stuff in argc-argv style, which
is hard to do using your approach. (I could use
a 'std::vector<char*>', but this again leads to
problem #1...)
Anyway, thank you for thinking about my problem!
[...]
Mike

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
Nov 17 '05 #4


"Hendrik Schober" wrote:
mike <mi**@discussions.microsoft.com> wrote:
[...]
Because there are no array literals in C++ (like Andrew said) [...]


Andrew, Mike,

thanks for the explanation!
Too bad I haven't found a way to do this. I hoped I
could somehow easily put all the data for one test
into one line. But I think in the end I will have
to live with what I got...
[...] Play with this code a bit to see if it can do what
you want:
[...]


Thanks. There's two things I don't like about it:
1. Overloading the comma operator for some simple
test app seems a bit over the top to me. :)
2. I do need the stuff in argc-argv style, which
is hard to do using your approach. (I could use
a 'std::vector<char*>', but this again leads to
problem #1...)
Anyway, thank you for thinking about my problem!
[...]
Mike

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett

You are welcome. Maybe the next C++ std will have support for array literals
:)

Cheers
Nov 17 '05 #5
Hendrik Schober wrote:
Thanks. There's two things I don't like about it:
1. Overloading the comma operator for some simple
test app seems a bit over the top to me. :)
2. I do need the stuff in argc-argv style, which
is hard to do using your approach. (I could use
a 'std::vector<char*>', but this again leads to
problem #1...)
Anyway, thank you for thinking about my problem!


Don't forget:
http://www.boost.org/libs/assign/doc/index.html

Tom
Nov 17 '05 #6
mike <mi**@discussions.microsoft.com> wrote:
[...] Maybe the next C++ std will have support for array literals
This
http://anubis.dkuug.dk/jtc1/sc22/wg2...2003/n1509.pdf
looks promissing. :)
Cheers

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett

Nov 17 '05 #7
Tom Widmer <to********@hotmail.com> wrote:
[...]
Don't forget:
http://www.boost.org/libs/assign/doc/index.html
Cool.
(They do overload the comma operator.)
Tom

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pedro Miguel Carvalho | last post: by
10 posts views Thread by Bart Goeman | last post: by
4 posts views Thread by ccdrbrg | last post: by
5 posts views Thread by David Mathog | last post: by
14 posts views Thread by Carramba | last post: by
4 posts views Thread by benn686 | last post: by
27 posts views Thread by Nate Eldredge | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.