opCast using in template struct
Ali Çehreli
acehreli at yahoo.com
Sat Dec 8 07:50:07 PST 2012
On 10/19/2012 06:38 AM, Oleg wrote:
> Problem solved partially.
>
> http://dpaste.dzfl.pl/e7871a01
>
> in structs I use fix length arrays declarations, and alias its to
> structs, but not allowed casting to fix length arrays.
> I want check array length in compile time
>
> auto opBinary(string op,E)( E[DLen] b ) // fix length array
E[DLen] is good above. You can also consider taking by 'const ref' if
DLen is too large. Otherwise, being value-types, fixed-length arrays
normally get copied to functions.
> if( ( op == "+" || op == "-" ) && is( E : T ) )
> {
> // without this checking in runtime
> //if( DLen != b.length )
> // throw new Exception("bad length");
Yes, that is not needed anymore because now it is known that b.length is
always DLen.
> auto res = VecT(this);
> foreach( i, ref m; mixin( "res." ~ DName ) )
> mixin( "m " ~ op ~ "= b[i];" );
> return res;
> }
>
> if I write E[DLen] I have errors like this
>
> Error: template
> opop.vec!("xyz").vec.arrayMath!("data",3LU,double,vec!("xyz")).opAssign
> does not match any function template declaration
>
> Error: template
>
opop.vec!("xyz").vec.arrayMath!("data",3LU,double,vec!("xyz")).opAssign(E)
> if (is(E : T)) cannot deduce template function from argument types
> !()(int[])
The problem is that array literals are slices, not fixed-length arrays.
pragma(msg, typeof([1,2,3]));
prints
int[]
> but declaration like this allowed
> int[4] f = [1,2,3,4];
Because in that case the type of f is explicitly int[4]. The following
would not be:
auto f = [1,2,3];
The type of f is int[].
Of course it is possible to support both arrays and slices, and avoid
the check by something like isStaticArray, but your callers will have to
be aware of the fact that array literals are slices.
Ali
More information about the Digitalmars-d-learn
mailing list