How can I prevent an unnamed struct \ union?

I am creating a class that has a union for its matrix data, however I can only get it when I don't have a name for struct \ union. However, with a higher warning level (four at visual studio), I will warn by saying

warning C4201: nonstandard extension used : nameless struct/union

I looked into it, and it seems I can’t find a way to prevent this. In any case, I know that this will lead to another compiler error related to the declaration of one or the other. How can I prevent this warning from being received and bring it into conformity with the standards without disabling this warning.

    union
    {
        struct
        {
            F32 _11, _12, _13, _14;
            F32 _21, _22, _23, _24;
            F32 _31, _32, _33, _34;
            F32 _41, _42, _43, _44;
        };
        F32 _m[16];
    };

(Yes, I know that there are matrix libraries available. Please do not include this in the "use xxx library" drive, I am doing this to expand my knowledge of C ++. ")

+5
3

. ++, .

union
{
    struct foo
    {
        F32 _11, _12, _13, _14;
        F32 _21, _22, _23, _24;
        F32 _31, _32, _33, _34;
        F32 _41, _42, _43, _44;
    } bar;
    F32 _m[16];
};

/, bar.

F32& _11 = bar._11;
F32& _12 = bar._12;

, . . bar._11, .


/ (sorta):

struct mat 
{
  struct foo 
  {
    friend class mat;
    private:
      F32 _11, _12, _13, _14;
      F32 _21, _22, _23, _24;
      F32 _31, _32, _33, _34;
      F32 _41, _42, _43, _44;
  };
  union
  {
    foo bar;
    F32 _m[16];
  };
};
+6

, , , #pragma warning :

#pragma warning(disable : 4201)

, :

#pragma warning(default : 4201)

. MSDN .

+3

You have this warning not about the internal structure, but about the union itself. Try the following:

union Mat    // <-------
{
    struct
    {
        F32 _11, _12, _13, _14;
        F32 _21, _22, _23, _24;
        F32 _31, _32, _33, _34;
        F32 _41, _42, _43, _44;
    };
    F32 _m[16];
};

Mat mat;
mat._11 = 42;
F32 x = mat._22;
mat._m[ 3 ] = mat._33;
-1
source

All Articles