handmade.network » Forums » Help handling data in a non OOP way
nyeecola
Italo
9 posts
#11825 Help handling data in a non OOP way
3 weeks ago Edited by Italo on May 4, 2017, 10:46 a.m.

One more thing, I tried using anonymous structs inside anonymous unions and got an error that "its forbidden by ISO C++".

Then, I tried to give the struct a name and leave only the union anonymous, but I got another error now: "an anonymous union can only have non-static data members".

Should I just disable these errors? I'm kinda worried that this will make my code not work on other compilers or cause some undefined behavior. What should I do here?

EDIT: Nevermind, I'll just use Delix's suggestion.
mmozeiko
Mārtiņš Možeiko
1315 posts
1 project
#11835 Help handling data in a non OOP way
3 weeks ago Edited by Mārtiņš Možeiko on May 4, 2017, 5:55 p.m.

nyeecola:
What do you mean by name collisions?

This:
1
2
3
4
5
union U
{
  struct { float x, y, z; };
  struct { float x, y, z, w; };
}

If you have U.x, which x it is?

nyeecola:
One more thing, I tried using anonymous structs inside anonymous unions and got an error that "its forbidden by ISO C++".

Those both are not errors, but warnings. If you want to target ISO C++ without any compiler extensions, then anonymous unions are not in standad, you cannot use them. That said, all reasonable compilers (MSVC, gcc, clang) support them perfectly fine. Unless you'll need to use some crazy embedded platform with custom C/C++ compiler, you'll have no issues.