関連:プリプロセッサディレクティブの#ifと非型テンプレートパラメータ
- Reason for using non-type template parameter instead of regular parameter?
- What is angle brackets for argument values, and what is it used for?
私はこれを行うことができますか?
template <int N> union Vector
{
float e[ N ] ;
// If N is 3, define x,y,z components
#if N==3
struct { float x,y,z ; } ;
#elif N==2
struct { float x,y ; } ;
#endif
} ;
// use
int main()
{
Vector<2> xy ;
xy.e[ 0 ] = 5 ;
xy.e[ 1 ] = 2 ;
xy.x = 2 ;
Vector<3> xyz ;
xyz.z = 4 ;
}
Livin '、そのユーザーIDまで、eh :) – bobobobo