[Issue 337] New: IFTI and overloading are incompatible

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 10 08:16:39 PDT 2006


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

           Summary: IFTI and overloading are incompatible
           Product: D
           Version: 0.166
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: mclysenk at mtu.edu


Two overloaded template functions with the same template signature will cause a
name conflict.  Here is an example:

//----

import std.stdio;

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

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

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

//----

In this situation there is a name conflict between the two overloads of
testfunc since their templates have the exact same signature.  One solution is
to wrap them both inside a single template as follows:

//----
template testfunc(T)
{
    void testfunc(T a) { ... }
    void testfunc(int x, T a) { ... }
}

//----

However, testfunc is no longer considered a function template after such an
operation, and IFTI is no longer applied.  This approach also prevents other
modules from performing an overload on testfunc, such as declaring something
like:

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

This will result in a name conflict with the previous declarations.  One
solution is to add all of the types in testfunc to the template specification
like this:

//----
void testfunc(Tx : int, Ta)(Tx x, Ta a)
{
    writefln("x = %d, a = %s", x, a);
}
//----

Unfortunately this gives the error:

modname.d(xx): template modname.testfunc(Tx : int,Ta) specialization not
allowed for deduced parameter Tx


The situation becomes even worse if the type of Tx is deduced from Ta, such as
a delegate or another template instance:

void foreach_wrapper(T)(T[] a, void delegate(inout T) dg);
void foreach_wrapper(T)(T[] a, void delegate(int, inout T) dg);


-- 




More information about the Digitalmars-d-bugs mailing list