Learn template by math vector class

Darren Darren_member at pathlink.com
Thu Jun 22 01:33:22 PDT 2006


In article <e7dgpn$5q$1 at digitaldaemon.com>, Hasan Aljudy says...
>
>I think you can use static if:
>
>static if( size < 3 ) //no z component
>{
>     pragma( msg, "2d vectors don't have a z component" ); //tell 
>compiler to print this msg
>     static assert(false); //halt compiler
>}

Wow.  Thanks, that just worked, though I did it a bit different...

Like this:

class vector...
{
..
static if ( size >= 1 )
{	
T x() { return p[ 0 ]; }
void x( T v ) { p[ 0 ] = v; }
}
..
}

Now is there a way to generalize this pattern with something like mixins?
ie. do something like 

template elementAccessor( name, int index )
{
T name() { return p[ index ]; }
void name( T v ) { p[ index ] = v; }
}

mixin elementAccessor!( x, index = 0 );
mixin elementAccessor!( y, index = 1 );
mixin elementAccessor!( z, index = 2 );
mixin elementAccessor!( w, index = 3 );






More information about the Digitalmars-d-learn mailing list