www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1789] New: Template overloading doesn't work

reply d-bugmail puremagic.com writes:

           Summary: Template overloading doesn't work
           Product: D
           Version: 1.025
          Platform: PC
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: aarti interia.pl

void pattern(T)(T v) {}

T pattern(T)() {return T.init;}

void main() {


Above code results in compile time error:

quicktest.d(6): template instance pattern!(int) matches more than one template
declaration, pattern(T) and pattern(T)
quicktest.d(6): Error: template instance 'pattern!(int)' is not a variable
quicktest.d(6): Error: function expected before (), not pattern!(int) of type


As you see it is standard D setter/getter approach with templates.

What's more interesting, when we call pattern template function with IFTI it
calls proper function:

pattern(5); //calls setter

pattern; // is error

BTW: I hope that in DMD 2.0
will be also allowed. Currently it doesn't work - you have to write writefln();


Similar to: #1193

Jan 17 2008
parent d-bugmail puremagic.com writes:

bugzilla digitalmars.com changed:

           What    |Removed                     |Added
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX

------- Comment #1 from bugzilla digitalmars.com  2008-08-29 00:29 -------
That's the way it is designed to work, overloading of templates with the
template arguments overloads based on only the template argument list. You can
achieve what you wish with the following:

template pattern(T)
    void foo(T v) {}
    T foo() { return T.init; }

alias pattern!(int).foo bar;

void main() {

    pattern!(int).foo = 5;
    int x = pattern!(int).foo;

    bar = 5;
    int y = bar;

Aug 28 2008