[Issue 337] Function Templates cannot be overloaded

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 26 03:47:38 PDT 2006


http://d.puremagic.com/issues/show_bug.cgi?id=337


brunodomedeiros+bugz at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|IFTI and overloading are    |Function Templates cannot be
                   |incompatible                |overloaded




------- Comment #1 from brunodomedeiros+bugz at gmail.com  2006-09-26 05:47 -------
Saying this is IFTI related isn't exactly accurate. Since the problem occurs at
template definition, no instantiation is need (implicit or explicit). 

I'm not sure this is a bug (an enhancement perhaps). The spec is not explicit,
but only functions can be overloaded, or templates with different
specializations. Templated functions are templates foremost (not functions), so
they can only be overloaded if the specialization is different, which is not
the case.
However I think the following workarounds work (without loss of functionality)
:

* For overload based on number of parameters only:
--------
import std.stdio;

void testfunc(T)(T a) {
    writefln("a = %s", a);
}

void testfunc(DUMMY = void, T)(int x, T a) {
    writefln("x = %d, a = %s", x, a);
}

void main() {
    testfunc("test1");
    testfunc(10, "test2");
}
----

* For any kind of overload (just make dummy specializations):
--------
void foreach_wrapper(T)(T[] a, void delegate(inout T) dg) { }
void foreach_wrapper(DUMMY : int=int, T)(T[] a, void delegate(int, T) dg) {
    pragma(msg, "Second specialization");
}
void foreach_wrapper(DUMMY : char=char, T)(T[] a, void delegate(char, T) dg) {
    pragma(msg, "Third specialization");
}

void main()
{
    void delegate(int, inout byte) dg1;
    foreach_wrapper(new byte[3], dg1);

    void delegate(char, inout byte) dg2;
    foreach_wrapper(new byte[3], dg2);
}
----


-- 




More information about the Digitalmars-d-bugs mailing list