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,724 of 33,346   
   fmatthew5876 to All   
   Re: Fun with unions   
   12 Dec 12 19:42:48   
   
   From: fmatthew5876@googlemail.com   
      
   Section 9.2.19 of the standard says   
   "If a standard-layout union contains two or more standard-layout structs that   
   share a common initial sequence,   
   and if the standard-layout union object currently contains one of these   
   standard-layout structs, it is permitted   
   to inspect the common initial part of any of them. Two standard-layout structs   
   share a common initial   
   sequence if corresponding members have layout-compatible types and either   
   neither member is a bit-field or   
   both are bit-fields with the same width for a sequence of one or more initial   
   members."   
      
   So for   
      
   union A {   
   struct {   
   float x, y;   
   int z;   
   };   
   struct {   
   float r, g;   
   char b;   
   };   
   };   
      
   If I understand the "common initial sequence" property correctly this means   
   you can safely and standards compliantly read and write to x and r, y and g.   
      
   But how about this:   
      
   union B {   
   float x;   
   struct {   
   float r;   
   };   
   };   
      
   Do the members x and the anonymous struct containing r have a "common initial   
   sequence" namely of a single float?   
      
      
   --   
         [ 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