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(cons t 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 7 1573
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 inititializatio n syntax.
-- Andrew Bell an************@ gmail.com
"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(cons t mytest (&arr)[N]);
int mainx()
{
testParser(v1);
testParser(v2);
return 0;
}
Cheers,
Mike
mike <mi**@discussio ns.microsoft.co m> 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<ch ar*>', 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
"Hendrik Schober" wrote: mike <mi**@discussio ns.microsoft.co m> 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<ch ar*>', 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
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<ch ar*>', 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
mike <mi**@discussio ns.microsoft.co m> 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
Tom Widmer <to********@hot mail.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 shed some light on this.
class test {
public:
test( const float T ) {
}
|
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
structures are read only)
Ideally one should be able to put the redundant
information there automatically so no mistakes are possible, but in a lot
of case I see no way how to do it.
|
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;
};
|
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}
|
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 to get the
perl code intact through the C preprocessor.) The issue is that
many of these strings are quite long, which causes gcc to throw
these sorts of warnings:
scripts.h:1: warning: string length '4918' is greater than the length
'4095'...
| |
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;
int c;
|
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 = 100;
unionMember.union1.field2 = 200 };
fullStructType var2 = { { unionMember.union2 = 300 } ,
{ unionMember.union2 = 400 } };
|
by: Nate Eldredge |
last post by:
Consider the following pseudo-code:
#include <opaque.h>
struct foo {
int a;
opaque_t op;
int b;
};
|
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;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |