From: cassio.neri@googlemail.com   
      
   On Thursday, November 22, 2012 11:26:06 PM UTC, Frank Birbacher wrote:   
   > Hi!   
   > So, how about this implementation:   
   >   
   > template   
   > struct foo   
   > {   
   > T head;   
   > foo tail;   
   > };   
   >   
   > template   
   > struct foo   
   > {   
   > T head;   
   > };   
   > //checks pass on clang 3.2, gcc 4.7, gcc 4.6:   
   >   
   > typedef foo Foo10;   
   >   
   > static_assert(std::is_pod::value, "must be pod");   
   > static_assert(sizeof(Foo10) == sizeof(int) * 10, "wrong size");   
   >   
   > As it is a POD for any POD T, it should be layout-compatible with a   
   > T[N], right?   
   >   
   > Frank   
      
   I believe:   
      
   1) It might fix the issue raised by Zhihao.   
      
   2) It doesn't fix the issue raised by Daniel. If I understood him correctly   
   (please, correct me if I'm wrong) the compiler might add some padding to   
   foo just after head (or tail, if N != 1). However, despite legal, this   
   seems very unlikely to me since any padding required to align head is likely   
   to be added to T rather than to foo. (If this argument is correct, by   
   recurrence, we conclude that there's no padding added to foo after   
   tail.)   
      
   In addition, T is already sized and aligned in order to have   
   sizeof(T[N])/sizeof(T) == N. I guess the same could be said for foo   
   that is, sizeof(foo)/sizeof(T) == N.   
      
   Unless the compiler decides to add extra bytes to foo for reasons   
   other than alignment. (Would that be allowed? Recall that foo has no other   
   non-static data members, no virtual methods and no base classes.)   
      
   3) (Less relevant but worth mentioning) If T isn't POD, then foo isn't   
   POD. Hence, even if std::is_pod>::value == true implies that   
   foo is layout-compatible with T[N], I don't think the converse is true.   
   I suppose that testing the sizes (as you did) is more conclusive.   
      
   Thanks for all your comments. I've changed my implementation which is now   
   based on std::aligned_storage and std::alignment_of.   
      
   Cassio.   
      
      
   --   
    [ 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)   
|