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

Signature
SpamTrap@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
andrew.bell.ia@gmail.com - 09 Jun 2005 14:36 GMT
> Hi,
>
[quoted text clipped - 27 lines]
> 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
andrew.bell.ia@gmail.com
mike - 09 Jun 2005 15:51 GMT
> 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
Hendrik Schober - 11 Jun 2005 17:29 GMT
> [...]
> 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

Signature
SpamTrap@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
mike - 13 Jun 2005 14:30 GMT
> > [...]
> > Because there are no array literals in C++ (like Andrew said) [...]
[quoted text clipped - 24 lines]
>
> Schobi
You are welcome. Maybe the next C++ std will have support for array literals
:)
Cheers
Hendrik Schober - 14 Jun 2005 13:01 GMT
> [...] Maybe the next C++ std will have support for array literals
This
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1509.pdf
looks promissing. :)
> Cheers
Schobi

Signature
SpamTrap@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
Tom Widmer - 13 Jun 2005 15:49 GMT
> Thanks. There's two things I don't like about it:
> 1. Overloading the comma operator for some simple
[quoted text clipped - 4 lines]
> problem #1...)
> Anyway, thank you for thinking about my problem!
Don't forget:
http://www.boost.org/libs/assign/doc/index.html
Tom
Hendrik Schober - 14 Jun 2005 13:02 GMT
> [...]
> Don't forget:
> http://www.boost.org/libs/assign/doc/index.html
Cool.
(They do overload the comma operator.)
> Tom
Schobi

Signature
SpamTrap@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