www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template function limitations

reply Sean Kelly <sean f4.ca> writes:
I've been playing with template functions over the past few days and 
have been wondering about how much detail they should be able to infer 
about a parameter.  In particular, template template parameters seem to 
have only limited support.  For example:

     class One(T) {}
     class Two(T, U) {}

     void fnA(T)( One!(T) val ) {}
     void fnB(T,U)( T!(U) val ) {}
     void fnC(T...)( Two!(T) val ) {}


     void main()
     {
         fnA( new One!(int) );
         // works

         fnB( new One!(int) );
         // test.d(14): template test.fnB(T,U) does not match any
         //             template declaration
         // test.d(14): template test.fnB(T,U) cannot deduce template
         //             function from argument types (One)


         fnB!(One,int)( new One!(int) );
         // test.d(21): template instance fnB!(One,int) does not match
         //             any template declaration
         // test.d(21): Error: template instance 'fnB!(One,int)' is not
         //             a variable


         fnC( new Two!(int,int) );
         // test.d(27): template test.fnC(T...) does not match any
         //             template declaration
         // test.d(27): template test.fnC(T...) cannot deduce template
         //             function from argument types (Two)
     }

Which of the above functions /should/ work?


Sean
Sep 19 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 9/19/07, Sean Kelly <sean f4.ca> wrote:
      void fnB(T,U)( T!(U) val ) {}
Shouldn't that be void fnB(alias T,U)( T!(U) val ) {} ? I could be wrong, but I thought "alias" was necessary if you want a template template parameter.
Sep 19 2007
parent Sean Kelly <sean f4.ca> writes:
Janice Caron wrote:
 On 9/19/07, Sean Kelly <sean f4.ca> wrote:
      void fnB(T,U)( T!(U) val ) {}
Shouldn't that be void fnB(alias T,U)( T!(U) val ) {} ? I could be wrong, but I thought "alias" was necessary if you want a template template parameter.
Perhaps, but I get the same error with and without "alias." Sean
Sep 19 2007