class template specialization and inheritance
Fawzi Mohamed
fmohamed at mac.com
Wed May 14 02:21:08 PDT 2008
On 2008-05-14 00:55:41 +0200, mki <none at none.com> said:
> Hello!
>
> I just discovered the template syntax of D. I am very exited about its
> simplicity compared to C++.
>
> Now I ran into a template behavior I do not understand. This code:
>
> *** begin code 1 ***
> import std.stdio;
>
> class A { }
>
> class B : A { }
>
> class C(T:A) {
> static void tellMe() {
> writefln("derived from A.");
> }
> }
>
> class C(T) {
> static void tellMe() {
> writefln("generic.");
> }
> }
>
> void main() {
> C!(A).tellMe();
> C!(B).tellMe();
> C!(int).tellMe();
> }
> *** end code 1 ***
>
> as expected produces the output:
>
> derived from A.
> derived from A.
> generic.
>
>
> But this code
> *** begin code 2 ***
> import std.stdio;
>
> class A(T) { }
>
> class B(T) : A!(T) { }
>
> class C(TT:A!(T)) {
> static void tellMe() {
> writefln("derived from A!(T).");
> }
> }
>
> class C(T) {
> static void tellMe() {
> writefln("generic.");
> }
> }
>
> void main() {
> C!(A!(int)).tellMe();
> C!(B!(int)).tellMe();
> C!(int).tellMe();
> }
> *** end code 2 ***
>
> gives the output:
> derived from A!(T).
> generic.
> generic.
>
>
> In the second line I would expect the output "derived from A!(T)", like
> in the example of code 1. My feeling is that for C!(B!(int))
> 'class C(TT:A!(T))'
> with TT=B!(T) and T=int should be a better specialization than
> 'class C(T)'
> with T=B!(T).
>
> Why is 'class C(T)' chosen here?
It looks like a bug to me, I would write a report on
http://d.puremagic.com/issues/
maybe you should rewrite the messages to "T is derived from ...",
because C is not derived from those classes, and the message is
misleading.
Fawzi
More information about the Digitalmars-d
mailing list