Initializing a struct with an array literal

  • Thread starter Hendrik Schober
  • Start date
H

Hendrik Schober

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

--
(e-mail address removed) 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
 
A

andrew.bell.ia

Hendrik said:
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
(e-mail address removed)
 
G

Guest

Hendrik Schober said:
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
 
H

Hendrik Schober

mike said:
[...]
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

--
(e-mail address removed) 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
 
G

Guest

Hendrik Schober said:
mike said:
[...]
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

--
(e-mail address removed) 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
 
T

Tom Widmer

Hendrik said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top