www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template subclass as template parameter

reply Ivo Jimenez <ivo.jimenez gmail.com> writes:
--0016e65684bebb8d53048d96c04a
Content-Type: text/plain; charset=UTF-8

Hi,

I'm new to D. I have the following:

   class A(T) { ... }
   class B(T) : A!(T) { ... }

   class C(T, U : A!(T)) { ... }

And I'm trying to do this

   void main()
   {
       C!(double, B!(double)) var;
   }

but dmd complains:
   Error: template instance C!(double,B) does not match template declaration
C(T,U : A!(T))

If I change the line of main() by

   void main()
   {
       C!(double, A!(double)) var;
   }

it works OK. Does the fact that B(T) is a child of A(T) imply that I can use
B(T) as a template parameter wherever A(T) is expected?

Could you please point to where I'm doing things wrong. I'd really
appreciate your feedback.

Thanks,
Ivo

--0016e65684bebb8d53048d96c04a
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,<br><br>I&#39;m new to D. I have the following:<br><br>=C2=A0=C2=A0 clas=
s A(T) { ... }<br>=C2=A0=C2=A0 class B(T) : A!(T) { ... }<br><br>=C2=A0=C2=
=A0 class C(T, U : A!(T)) { ... }<br><br>And I&#39;m trying to do this<br><=
br>=C2=A0=C2=A0 void main()<br>=C2=A0=C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0=C2=A0 C!(double, B!(double)) var;<br>=C2=A0=C2=A0 }<br=
<br>but dmd complains:<br>=C2=A0=C2=A0 Error: template instance C!(double,=

e line of main() by<br><br>=C2=A0=C2=A0 void main()<br> =C2=A0=C2=A0 {<br>=C2=A0 =C2=A0 =C2=A0=C2=A0 C!(double, A!(double)) var;<br=
=C2=A0=C2=A0 }<br><br>it works OK. Does the fact that B(T) is a child of A=

cted?<br><br>Could you please point to where I&#39;m doing things wrong. I&= #39;d really appreciate your feedback.<br> <br>Thanks,<br>Ivo<br> --0016e65684bebb8d53048d96c04a--
Aug 11 2010
parent BCS <none anon.com> writes:
Hello Ivo,

 class C(T, U : A!(T)) { ... }
 
 And I'm trying to do this
 
 void main()
 {
 C!(double, B!(double)) var;
 }
 but dmd complains:
 Error: template instance C!(double,B) does not match template
 declaration C(T,U : A!(T))

The way you have it is asking for an exact match. I would have to look it up but I think you can make what you want work with something like this: class C(T, U) if(is(U : A!(T))) { ... } That takes any types T and U and then checks if U can convert to A!(T) (I haven't tested that so I might have the syntax wrong.) -- ... <IXOYE><
Aug 11 2010