home bbs files messages ]

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,606 of 33,346   
   troplin@googlemail.com to Michael Kilburn   
   Re: initializing array using another arr   
   25 Oct 12 00:53:21   
   
   d43d2832   
   { Please limit your text to fit within 80 columns, preferably around 70,   
     so that readers don't have to scroll horizontally to read each line.   
     This article has been reformatted manually by the moderator. -mod }   
      
   Michael Kilburn  wrote:   
   > Thank you (and everybody else) for answers. I'll follow suggestion   
   > received in personal email and try to explain the problem I was trying   
   > to solve instead of boiling it down to bare minimum. You might find it   
   > more interesting:   
   >   
   > Given:   
   > - bunch of arrays declared elsewhere (outside of my control):   
   >      char const S1[] = "ABC";   
   >      char const S2[] = "DEF";   
   >   
   > - template that looks like this:   
   >      template class Value   
   >      {   
   >          ... // quite a bit of logic is in these dots   
   >          static char const* TAG;   
   >      };   
   >   
   > every instance of this template has a declaration like this (in some   
   > translation unit):   
   >      template<> char const* Value::TAG = "int";   
   > some of them look like this:   
   >      template<> char const* Value::TAG = S1;   
   >   
   > Needed:   
   >   
   > - to convert TAG from pointer to array, i.e. solution should look smth   
   > like:   
   >      // .h   
   >      template class Value   
   >      {   
   >          ...   
   >          static char const TAG[];   // note that array size is not   
   >                                     // associated with variable yet   
   >      };   
      
   I still don't understand why you need that. A C string is not delimited   
   by the length of the array but by the terminating 0.   
   Smart compilers may even optimize away strlen if the string value is   
   known at compile time.   
      
   >      // strangely enough compiler treats these declarations as typical   
   >      // "extern char const S[N]" and associates array length (e.g.   
   >      // sizeof(S1)) with given variable instance (e.g. Value::TAG)   
      
   Strange indeed, how is that supposed to work for other translation units?   
   Or will it those just assume a pointer, like in a function parameter?   
      
   >      template<> char const Value::TAG[sizeof("int")];   
   >      template<> char const Value::TAG[sizeof(S1)];   
   >   
   >      // after this point all users could see array size   
   >      // (i.e. could use sizeof)   
   >   
   >      // .cpp   
   >      // this works fine   
   >      template<> char const Value::TAG[sizeof("int")] = "int";   
   >      // this doesn't compile :-\   
   >      template<> char const Value::TAG[sizeof(S1)] = S1;   
   >   
   > I am aware that this code tries to create new array instance (i.e.   
   > we'll end up with a copy of the same S1 string in two different   
   > translation units) -- this is fine, because I know my linker will   
   > collapse them into one later (no shared libs).   
   >   
   > In any case code above does not work. After I've replaced array with a   
   > reference to array, I've discovered that forward declarations used to   
   > "add" array size info to variable stopped working -- compiler refuses   
   > to accept them (same for stuff in .cpp -- now it complains about   
   > difference between 'reference to array[]' vs 'reference to array[8]',   
   > even though it was totally cool with 'array[]' vs 'array[8]').   
      
   Well that's what I would expect and how it works for non-array types.   
   So you complain about arrays not being first class citizens and on the   
   other hand you also complain that they actually _do_ behave like first   
   class citizens in the case of "reference to array"?   
      
   Tobi   
      
      
   --   
         [ 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