May 18, 2008
mki wrote:
> I don't see why the original
> class C(TT:A!(T))
> shouldn't be legal.
> 
> in C++ style syntax I would have
> 
> //primary template
> template<typename T>
> class C {};
> 
> //template specialization of C
> template<typename TT, typename T>
> class C<TT:A<T> > ... ;
> 
> (Of course this isn't legal either, because C++ doesn't have the colon-syntax.)
> 
> Now D does away with primary templates, so the first part can be skipped completely in D.
> Furthermore, D does not need the line
> template<typename TT, typename T>
> because the template parameters of the specialization can be completely deduced from the expression C<TT:A<T> >. All undeclared symbols are template parameters.
> 
> So to my understanding,
> C(TT:A(T))
> should be legal D and should have the sense I indicated with the C++ style syntax above. Also notice that there is no compiler error message on this expression.

From http://www.digitalmars.com/d/1.0/template.html
"""
Deduction from a specialization can provide values for more than one parameter:

template Foo(T: T[U], U)
{
    ...
}

Foo!(int[long])  // instantiates Foo with T set to int, U set to long
"""

But (for D1 at least) that doesn't work with argument deduction:

void fBaz(T: T[U], U)(T[U] x) {
    writefln("typeof(T)=",typeid(T)," typeof(U)=",typeid(U));
}
...
int[long] z;
fBaz(z);

>> partialifti.d(24): template partialifti.fBaz(T : T[U],U) specialization not allowed for deduced parameter T


> For my programming purpose, the important thing is that C(TT:A(T)) is a specialization of C(T), but the suggested C(TT:A(T),T) is _not_.

According to the documentation D thinks it is.  Do you have evidence to the contrary?

> For this reason, I really need the first variant, and not the second one.
>
> I would like to hear further opinions on this. I my understanding correct, or not?

Sadly the argument deduction (IFTI) implementation is not very complete.  It only works in very specific situations.

--bb
May 24, 2008
Since I got no further answer, I decided to do so and filed a bug report: http://d.puremagic.com/issues/show_bug.cgi?id=2126

~mki

Fawzi Mohamed Wrote:

> On 2008-05-14 00:55:41 +0200, mki <none@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
1 2
Next ›   Last »