Passing a generic struct as parameter

Simen Kjaeraas simen.kjaras at gmail.com
Thu Jun 30 17:29:58 PDT 2011


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.

-- 
   Simen


More information about the Digitalmars-d-learn mailing list