I’m coding a Vec3 class, and for optimization purpose, I make it with no constructor.
I also would like to be able to access its members as x, y, z OR as r, g, b OR as a tab.
Easy, you might think : use a union
template <typename T> struct Vec3_t
{
union
{
T val[3];
struct { T x, y, z; };
struct { T r, g, b; };
};
};
Then, since I have no ctor, I would like to initialize it like this :
Vec3_t<int> v = {1, 2, 3};
but I have to put double braces since I’m initializing a struct in a struct (like this : Vec3_t<int> v = {{1, 2, 3}} )
So, my question is : How could I do it so that I can have both access with different names, and initialization with one pair of braces ?
my try : having one union for each component, but then exit with the access as a table (one can always call &v.x and treat it as a float[3], but that’s kind of dirty… and not so safe I guess)
It cannot be done without a constructor, and it is a bad idea to avoid using a ctor at all costs.