www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - (non-)extern(C) function parameter vs templates

reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
This works:

     extern(C) alias F = void function();
               alias G = void function();

     void foo(F f) {}
     void foo(G g) {}

Indeed, the correct foo is called, depending on whether the 
passed function is extern(C) or not (although I must say that the 
syntax for declaring the alias F is not good, IMO; the extern(C) 
should come after the equal sign).

Yet, the following does not work:

     void foo(T)(F f) {}
     void foo(G g) {}

     Error: function foo conflicts with template foo(T)(F f).

or

     void foo(F f) {}
     void foo(T)(G g) {}

     Error: template foo(T)(E e) conflicts with function foo.

It this not incoherent? If non-templated functions can overload 
on extern(C)/non-extern(C) function parameter, why can't 
templated functions?
Sep 17 2013
next sibling parent =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
Nevermind. Lack of sleep --;;

:-)
Sep 17 2013
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 17 September 2013 at 13:36:24 UTC, Luís Marques wrote:
 This works:

     extern(C) alias F = void function();
               alias G = void function();

     void foo(F f) {}
     void foo(G g) {}

 Indeed, the correct foo is called, depending on whether the 
 passed function is extern(C) or not (although I must say that 
 the syntax for declaring the alias F is not good, IMO; the 
 extern(C) should come after the equal sign).

 Yet, the following does not work:

     void foo(T)(F f) {}
     void foo(G g) {}

     Error: function foo conflicts with template foo(T)(F f).

 or

     void foo(F f) {}
     void foo(T)(G g) {}

     Error: template foo(T)(E e) conflicts with function foo.

 It this not incoherent? If non-templated functions can overload 
 on extern(C)/non-extern(C) function parameter, why can't 
 templated functions?
This works for me on dmd git master: extern(C) alias F = void function(); alias G = void function(); void foo(T)(T g) {} void foo(F f) {} extern(C) void a(){} void b(){} void main() { foo(&a); foo(&b); } It used to be that templates and normal functions couldn't overload each other, now they can. I'm not sure when the change was made, it might only be in git master.
Sep 17 2013
parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
On Tuesday, 17 September 2013 at 13:50:20 UTC, John Colvin wrote:
 It used to be that templates and normal functions couldn't 
 overload each other, now they can. I'm not sure when the change 
 was made, it might only be in git master.
Ah, OK! I said nevermind because the following works (on 2.063.2): void foo(T)(F f) { } void foo(T)(E e) { } So I guess it's more subtle than that. Thanks!
Sep 17 2013
parent =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
On Tuesday, 17 September 2013 at 13:59:05 UTC, Luís Marques wrote:
 So I guess it's more subtle than that.
I meant it didn't have to do with extern(C).
Sep 17 2013