Is there a way to create alias for class/stuct filed in C++11 ? What I mean
I've got class
class Vector4
{
public:
float X,Y,Z,W;
}
I got an alias
typedef Vector4 Color;
Is there a way to create aliases for Vector4 X,Y,Z,W fields to work as Color R,G,B,A ?
Just define Vector4 like this, using anonymous unions (without anonymous structs, though they are a common extension).
typedef struct Vector4
{
union{float X; float R;};
union{float Y; float G;};
union{float Z; float B;};
union{float W; float A;};
} Vector4;
typedef Vector4 Color;
If you cannot redefine Vector4, you might instead just define a layout-compatible standard-layout class and use the dreaded reinterpret_cast
.
That works because standard layout classes having layout-compatible members are compatible.
struct Color
{
float R, G, B, A;
}
Standard quote:
A standard-layout class is a class that:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
— has no virtual functions (10.3) and no virtual base classes (10.1),
— has the same access control (Clause 11) for all non-static data members,
— has no non-standard-layout base classes,
— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
— has no base classes of the same type as the first non-static data member.A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class.
A standard-layout union is a standard-layout class defined with the class-key union.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With