473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1550
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.