opCast using in template struct

Oleg code.viator at gmail.com
Thu Oct 18 07:54:33 PDT 2012


Hello. How to cast template struct to itself?

struct vec(string S,T=double)
{
     T[S.length] data;
     auto opCast(string K,E)()
         if( S.length == K.length &&
              is( T : E ) )
     {
         vec!(K,E) ret;
         foreach( i, ref m; ret.data )
             m = data[i];
         return ret;
     }
}

unittest
{
     alias vec!"xyz" dvec3;
     alias vec!("rbg",float) fcol3;
     auto a = dvec3([1,2,3]);
     auto b = fcol3();
     b = a; #1
     assert( b.data == [1,2,3] );
}

#1 Error: cannot implicitly convert expression (a)
of type vec!("xyz") to vec!("rbg",float)


More information about the Digitalmars-d-learn mailing list