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,722 of 33,346   
   fmatthew5876 to All   
   Fun with unions   
   12 Dec 12 13:37:45   
   
   From: fmatthew5876@googlemail.com   
      
   Do you believe the following will work as intended according to the standard?   
   In otherwords, we should expect Vec4 to contain exactly 4 floats and   
   the various unions members become syntactic sugar.   
      
   For example if you want the middle two floats, you can do v.yz or v.gb.   
   Or if you do the following, you should expect the conditions to hold true.   
      
   v.xy = Vec2(3.0f, 8.0f);   
   v.x == 3.0f   
   v.y == 8.0f   
      
   Assume Vec2 and Vec3 are defined similarly.   
      
   template    
     struct Vec4 {   
       Vec4() = default;   
       explicit Vec4(T v);   
       Vec4(T xx, T yy, T zz, T ww);   
       Vec4(const Vec2& xy, T zz, T ww);   
       Vec4(T xx, const Vec2& yz, T ww);   
       Vec4(T xx, T yy, const Vec2& zw);   
       Vec4(const Vec2& xy, const Vec2& zw);   
       Vec4(const Vec3& xyz, T ww);   
       Vec4(T xx, const Vec3& yzw);   
       explicit Vec4(const T* v);   
      
       T& operator[](int i);   
       const T& operator[](int i) const;   
      
       enum Constants {   
         size = 4,   
       };   
       typedef T value_type;   
      
       union {   
         T m[Vec4::size];   
         Vec3 xyz;   
         Vec2 xy;   
         struct {   
           T x;   
           union {   
             Vec3 yzw;   
             Vec2 yz;   
             struct {   
               T y;   
               union {   
                 Vec2 zw;   
                 struct {   
                   T z;   
                   T w;   
                 };   
               };   
             };   
           };   
         };   
         Vec3 rgb;   
         Vec2 rb;   
         struct {   
           T r;   
           union {   
             Vec3 gba;   
             Vec2 gb;   
             struct {   
               T g;   
               union {   
                 Vec2 ba;   
                 struct {   
                   T b;   
                   T a;   
                 };   
               };   
             };   
           };   
         };   
         Vec3 uvs;   
         Vec2 uv;   
         struct {   
           T u;   
           union {   
             Vec3 vst;   
             Vec2 vs;   
             struct {   
               T v;   
               union {   
                 Vec2 st;   
                 struct {   
                   T s;   
                   T t;   
                 };   
               };   
             };   
           };   
         };   
       };   
     };   
      
      
   --   
         [ 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