I am building a class that has a union for its matrix data, however, I can only get it compile when I do not have a name for the struct\union. However, with a higher level warning level (four on visual studio) I will a warning saying
warning C4201: nonstandard extension used : nameless struct/union
I looked into it, and I don't seem to be able to find a way to prevent this. Anyway possible that I know of will cause a different compiler error related to the declaration of one or the other. How can I prevent getting this warning and make it conform to standards, without just disabling the 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 there is matric libraries available. Please do not turn this into a "use xxx library" discussion, I am doing this to expand my knowledge of C++".)
Naming it seems best. Anonymous unions are allowed in C++, just not structs.
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];
};
You can use references/macros to allow access without bar
.
F32& _11 = bar._11;
F32& _12 = bar._12;
Essentially the same as an anonymous struct. I don't really recommend this though. Use bar._11
if possible.
Private/public (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];
};
};