I have the following code declared in my header file:
//Integer Vector4D stuffs
typedef union {
size_t data[4];
struct {
size_t x;
size_t y;
size_t z;
size_t w;
};
struct {
size_t x;
size_t y;
size_t width;
size_t height;
};
} Vector4Di;
//End Integer Vector4D stuffs
I am compiling the code for Windows and Linux. I'm using Windows 10 Pro with WSL.
This is my compilation build output taken from Microsoft Visual Studio 2017:
1>------ Build started: Project: SDL, Configuration: Debug x64 ------
1>C:main_pc.cpp
1>1 File(s) copied
1>block.cpp
1>common.cpp
1>draw.cpp
1>game.cpp
1>input.cpp
1>main.cpp
1>Generating Code...
1>SDL.vcxproj -> C:\Users\tom_mai78101\Documents\VSProjects\SDL\x64\Debug\SDL.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Given the above build log, this shows that the build has succeeded when compiling.
That same source code, however, would throw errors in GCC:
$ make
clean ...
build ...
main.cpp
In file included from /mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/main.cpp:1:0:
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h: At global scope:
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:358:10: error: redeclaration of 'size_t <unnamed union>::<unnamed struct>::x'
size_t x;
^
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:352:10: note: previous declaration 'size_t <unnamed union>::<unnamed struct>::x'
size_t x;
^
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:359:10: error: redeclaration of 'size_t <unnamed union>::<unnamed struct>::y'
size_t y;
^
/mnt/c/Users/tom_mai78101/Documents/VSProjects/SDL/SDL/../SDL/game/common.h:353:10: note: previous declaration 'size_t <unnamed union>::<unnamed struct>::y'
size_t y;
^
The gist is, in GCC, it would complain about how size_t x;
and size_t y;
are both redeclared inside the union, Vector4Di
. But MSBuild doesn't throw errors in regards to this, and it successfully compiled the code.
To my knowledge, I believed unions with same types and same variable names should not be in conflict with another, especially when the variables are in a struct. Especially when compiling for C++11, where it was known that C++11 has better support for unions.
May I ask why it is throwing the error for GCC? And how should I fix this error so both MSBuild and GCC both compiled successfully?
Thanks in advance.