[Issue 798] New: Template function overloading problems
Sean Kelly
sean at f4.ca
Sat Jan 6 08:08:43 PST 2007
Template functions cannot be overloaded with normal functions. This is
by design.
d-bugmail at puremagic.com wrote:
>
> class Test {
> static void func(T)(T v) {}
> // static void func() {} //Error! Solution: static void
> func()() {}
> // static void func(int i, float f) {} //Error! Solution: static void
> func()(int i, float f) {}
static void func(T)(T v) {}
static void func()() {}
static void func(T1=void, T2=void)() {}
> static void opCall(T)(T t) {}
> static void opCall()() {}
> //Below line in fact conflicts with opCall()() as commenting it solves
> problem - no workaround known
> //static void opCall()(int i, float f) {}
Same as above. Templates with the same name and same number of
templates parameters cannot be overloaded unless one is a specialization
of the other. ie.
static void func(T, U)(T v1, U v2) {}
static void func(T, U)(T v1, U v2, int v3) {}
Doesn't work. But:
static void func(T, U)(T v1, U v2) {}
static void func(T, U : double)(T v1, U v2, int v3) {}
Works. The way around this problem is to add dummy template parameters
to alter the template parameter count to avoid unwanted collisions:
static void func(T, U)(T v1, U v2) {}
static void func(T, U, V=void)(T v1, U v2, int v3) {}
In the above, V is unused and serves only to change the name mangling of
the second template function.
Sean
More information about the Digitalmars-d-bugs
mailing list