1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
template< typename t >
class vec2i
{
private:
template< int a, int b, int c, int d >
class swizzle
{
private:
t v[2];
public:
operator vec2i()
{
return vec2i( v[a], v[b] );
}
};
public:
union
{
struct
{
t x, y;
};
swizzle < 0, 1, -2, -3 > xy;
t v[2];
};
vec2i( const t& a, const t& b ) : x( a ), y( b ) {}
explicit vec2i( const t& num ) : x( num ), y( num ) {}
vec2i() : x( 0 ), y( 0 ) {}
};
typedef vec2i<float> vec2;
typedef vec2i<double> dvec2;
template< typename t >
vec2i<t> operator+ ( const vec2i<t>& a, const vec2i<t>& b )
{
return vec2i<t>( a.x + b.x, a.y + b.y );
}
int main( int argc, char** argv )
{
vec2 a( 1, 2 );
a = a + a.xy; //generates no operator + found, saying candidate is the operator+ func
return 0;
}
|