www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overloading function with function templates

reply mstrlu <skateBBH yahoo.com> writes:
Hello,
I  just discovered that its not possible to write a function template with the
same identifier as an existing function ( even though the function arguments
are different ):

import std.stdio;

 
void test(int x)( int y ){
    writefln( "tmpl:", x, " val:", y);
}

int test(){
    return 15;
}

int main(){    
    writefln( test() );
    test!(2)(3);
    return 0;
}

with gdc 0.24 or dmd 1.023 I get "template_test.d(8): function
template_test.test conflicts with template template_test.test(int x) at
template_test.d(4)"

in c++ this doesn't seem to be a problem:

#include <iostream>

using namespace std;

template < int x > 
void test( int y ){
    cout << "tmpl:" << x << " val:" << y << endl;
}

int test(){
    return 15;
}

int main(){
    cout << test() << endl;
    test<2>( 3 );
}

works fine.

Isn't it possible to resolve the function templates before checking for
ambiguous identifiers in D?  Is there something in the documetation on this
subject that I did miss?

thank you
Dec 27 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
mstrlu wrote:
 Hello,
 I  just discovered that its not possible to write a function template with the
same identifier as an existing function ( even though the function arguments
are different ):
 
I couldn't find it written down anywhere in the docs, but it is definitely a well-known limitation of D templates. Another major one is that implicit instantiation is an all-or-nothing business. If you specify any template parameter you must specify them all. And finally you can't overload across modules. Yet. --bb
Dec 27 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fl1har$ihp$1 digitalmars.com...

 I couldn't find it written down anywhere in the docs, but it is definitely 
 a well-known limitation of D templates.
You can't do it. Yet. ;) Remember this is something else that was mentioned in the D con slides for D2.
 And finally you can't overload across modules.  Yet.
I thought overload sets were already implemented in the D2 compiler?
Dec 27 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to mstrlu,

 Hello,
 I  just discovered that its not possible to write a function template
 with the same identifier as an existing function ( even though the
 function arguments are different ):
 import std.stdio;
 
IIRC you can overload a template with some template args and a template with no args int Foo(T)(T arg){...} int Foo()(int a, int b){...}
Dec 27 2007