【发布时间】:2012-03-05 02:54:11
【问题描述】:
相关:
- 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 ;
}
【问题讨论】:
标签: c++ templates c-preprocessor