digitalmars.D.learn - Questions to template overload resolution
- Timon Gehr (21/21) Apr 12 2011 1) T foo(T:SomeClass)(T arg){..}
1) T foo(T:SomeClass)(T arg){..} It is specified on the main page that this template will match against instantiations with a subclass of SomeClass. Will this also duplicate the code? Or will the template work similar to the function T foo(SomeClass arg){..}? If yes, why should normal functions and function templates without type params be separated at all? 2) Why is this an error? Obviously, the second template is a better match. What is the rationale for requiring the clumsier f(T:double,U:int)(T a,U b) to f()(double a,int b) to allow proper overload resolution? import std.stdio; T f(T,U)(T a,U b){ return a; } T f()(double a,int b){ return a; } void main(){ writeln(f(1.2,1));//error } test.d(11): Error: template test.f(T,U) f(T,U) matches more than one template declaration, test.d(3):f(T,U) and test.d(6):f()
Apr 12 2011