Passing a generic struct as parameter

Zardoz luis.panadero at gmail.com
Thu Jun 30 23:58:32 PDT 2011


Well, the problem is that I must do the cast always, see :

// alias Matrix!(real,4) Mat4r;
// alias Vector!(float, 3) Vec3f;
// alias Vector!(real, 4) Vec4r;
// In Mat4r, VCol it's aliased to Vector!(real, 4)

auto tcol = Mat4r.IDENTITY; 

auto v = cast(tcol.VCol) Vec3f(1, 2 ,3 ); 
auto v2 = Vec4r(-1, -2 ,-3 ,-4);

tcol[1] = v2; // Do a compiler error
tcol[2] = v;

I get this error :
Error: function zmath.matrix.Matrix!(real,4).Matrix.opIndexAssign (real 
c, ulong row, ulong cl) is not callable using argument types (Vector!
(real,4),int)
Error: cannot implicitly convert expression (v2) of type Vector!(real,4) 
to Vector!(real,dim)

If I do cast(tcol.Vcol) to v2, this works.
Plus if I do a typeid(v1), typeid(v2) to see his types, I get this :
zmath.vector.Vector!(real,4).Vector  zmath.vector.Vector!(real,dim).Vector

On Fri, 01 Jul 2011 02:29:58 +0200, Simen Kjaeraas wrote:
> On Fri, 01 Jul 2011 01:39:53 +0200, Zardoz <luis.panadero at gmail.com>
> wrote:
> 
>> I have a parametrized struct (Vector!(T, dim)) that takes two
>> parameters (Type and a number). And made some Alias with defaults
>> parameters.
>>
>> In other struct (Matrix!(T, dim)), that uses these struct to represent
>> a matrix in column-major order. I have a internall alias for Vector
>> using internally (alias Vector!(T,dim_) VCol;) . The problem that I
>> have it's when I try to use opIndexAssign to assign to a column a
>> Vector. I try this :
>>
>> void opIndexAssign(Vector v, size_t j) {
>> 	if ( code that see if VCol if same type that v ) {
>> 		col[j] = v;
>> 	} else {
>> 		col[j] = cast (VCol) v;
>> 	}
>> }
>>
>> But not compile... I get this error : Error: struct
>> zmath.vector.Vector(T,ulong dim_) if (__traits (isFloating,T)) is used
>> as a type
>> Error: template instance zmath.matrix.Matrix!(float,2) error
>> instantiating
>>
>> So finally I try this :
>> /**
>> * Assigns a new column vector
>> */
>> void opIndexAssign(VCol v, size_t j) {
>> 	col[j] = v;
>> }
>>
>> But now I must do cast outside, even knowing that are same type. Plus
>> now I must keep VCol alias public.
>>
>> How should fix this, or What is the correct way of doing this ?
> 
> Private symbols in D are visible outside the defining module (and
> private symbols are accessible inside the same module), so having the
> alias private is no problem.
> 
> In other words, the latter solution is good, and should not require any
> casting or public alias.
> 
> Or have I perhaps misunderstood? Is there some other reason you need to
> cast?
> 
> 
> Also, the error message you get (Vector(...) is used as a type) is
> indicative of your referring to the Vector template rather than an
> instantiation. Struct templates in D behave as if defined thusly:
> 
> template Foo( T ) {
>      struct Foo {
>      }
> }
> 
> for a struct Foo( T ).
> 
> 
>> Note : I have a opCast for Vector that cast between Vectors with
>> different parameters and it's checked that works.
>>
>> Second Question : I'm thinking publish this small Vector/Quaternion/
>> Matrix lib that I made learning D2.. where I should put and how ? (and
>> I use Git)
> 
> GitHub, then? Or dsource.org.





-- 
Yep, I'm afraid that I have a blog : zardoz.es


More information about the Digitalmars-d-learn mailing list