Q:

Initializing union of structs nicely

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)

A:

If your compiler supports this feature of C++11, you can create a constructor that takes an std::initializer_list (I know you said you didn't want to have a constructor, but this solution requires one [but I don't think it cause a performance hit at all if you have optimisations on], sorry):
Vec3_t(std::initializer_list<T> list) : val(list) { }

Then you can construct a Vec3_t like you want:
Vec3_t<int> v = {1, 2, 3};

A:

It cannot be done without a constructor, and it is a bad idea to avoid using a ctor at all costs.

