Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,604 of 33,346    |
|    fmatthew5876 to All    |
|    Re: initializing array using another arr    |
|    24 Oct 12 11:48:30    |
   
   82e64cd0   
   From: fmatthew5876@googlemail.com   
      
   { Reformatted; please limit your lines to 70 characters, and do not   
   insert blank lines in quoted sections -mod }   
      
   > char const S1[sizeof("ABCD")] = "ABCD";   
   > char const S2[] = S1; // :-/   
      
   You don't need to specify the size of S1 here. It gets set correctly   
   when the array is initialized to the constant string.   
   char const S1[] = "ABCD";   
      
   > While this one is better, it is not ideal -- we end with   
   > (potentially vain) hope that compiler finds a way to optimize away   
   > one level of indirection introduced by reference.   
      
   Compile it and take a look at the dissassembly to see for yourself. A   
   reference can never be reseated so I don't see why the compiler would   
   be unable to optimize this case.   
      
   > template<> char const (&FooChar::M)[sizeof(S1)] = S1;   
   > template<> char const (&FooBool::M)[sizeof(S2)] = S2;   
      
   This is damn confusing, I would stay away unless there was a really   
   good reason to go down this route.   
      
   If both S1 and S2 are const then why do you need 2 arrays containing   
   the same data? Can't you just use S1 everywhere?   
      
   If you really just want a different name that could possibly be   
   changed to point to some other string later, you could do   
   #define S2 S1   
   but this doesn't work if you want to put S2 into a namespace.   
      
   Another option is this   
      
   const char S1[] = "BAR"   
   char S2[sizeof(S1)];   
      
   somewhere after main()..   
   memcpy(S2, S1, sizeof(S1);   
      
   But they won't be collapsed at link time and you'll have 2 copies of   
   the string.   
      
      
   If what you really want is the equivalent of this:   
   char const S1[] = "FOO";   
   char const S2[] = "FOO";   
   then without being able to make "FOO" a #define, the only option I can   
   think of is to write an external script into your build system that   
   parses the file that contains S1 and writes it onto the initializer   
   for S2.   
      
   They still might not be collapsed at link time. Especially if S1 is   
   located in a shared library.   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca