Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Initializing a struct with an array literal

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hendrik Schober - 09 Jun 2005 10:05 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.