472,811 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 1524
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pedro Miguel Carvalho | last post by:
Greetings. I have a class that I would like to initialize with a literal array but it is not accepted by the compiler. It works if I use a array variable but not a literal array. Can someone...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
5
by: David Mathog | last post by:
I'm looking at a program which stores perl scripts in an array. Each script is stored as a single entry in that array, and the whole set of them live in a single header file (escaped out the wazoo...
14
by: Carramba | last post by:
Hi! I have a big struct and I want to initialize it at once, but I get parse error before "{" compiler error. can't it by done? #include <stdio.h> #include <stdlib.h> typedef struct{ int b;...
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
27
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.