Thread overview
overloading function with function templates
Dec 27, 2007
mstrlu
Dec 28, 2007
Bill Baxter
Dec 28, 2007
BCS
December 27, 2007
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
December 28, 2007
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
December 28, 2007
"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?


December 28, 2007
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){...}