ありきたりな用途であれば、
同じ型が連続している構造体を、配列でアクセスしたり とか?
C++だとテンプレートも使えるので、下記のようにできる。
コード:
#include <iostream>
template<class Type>
union Vector3
{
struct {
Type x;
Type y;
Type z;
};
Type data[3];
};
int main()
{
using namespace std;
Vector3<int> vec = {1, 2, 3};
for (auto v : vec.data){
cout << v << endl;
}
cout << vec.x << endl;
cout << vec.y << endl;
cout << vec.z << endl;
return 0;
}