www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function Templates and Overloading

reply leoandru <leoandru_member pathlink.com> writes:
Hello fellow D coders,

I'm having a problem and I'm not sure if I'm missing something or trying to do
something stupid so I hope someone can show me the light here. I have several
function templates with the same name and different arguments like this example:

void main()
{
Foo!(int) foo = new Foo!(int)(1,2,3,4,5,6,7);

sort!(int)(foo); //sort natural ordering
sort!(int)(foo, (int a, int b) { return -(a - b); } );  //sort reverse order
}

class Foo (T)
{
this(T[] args ...)
{
}
}


void sort(T) (Foo!(T) foo)
{
// ...
}


void sort(T) (Foo!(T) foo, int delegate(T a, T b) compare)
{
// ...
}


void sort(T) (Foo!(T) foo, int function(T a, T b) compare)
{
// ...
}

The problem im having is when I try to compile something like this I get
conflicts errors like:

templates.d(24): template templates.sort(T) conflicts with templates.sort(T) at
templates.d(19)
templates.d(29): template templates.sort(T) conflicts with templates.sort(T) at
templates.d(19) 

using dmd 0.61 win xp.

Somehow I though this would have worked in that it would instantiate the correct
template based on the functions parameters and the number or parameters given.
Is there any work around apart from renaming the functions?

Thanks for any assistance.
Jul 03 2006
next sibling parent reply leoandru <leoandru_member pathlink.com> writes:
correction: I'm using dmd 0.161 not 0.61 as stated.

was reading on template specialization
http://www.digitalmars.com/d/template.html but the solution seem too cumbersome
for what I'm trying to do, which is to have a few function templates that
behaves just like their overloaded non-template counterparts. can that be done?
Jul 03 2006
parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
leoandru wrote:
 correction: I'm using dmd 0.161 not 0.61 as stated.
 
 was reading on template specialization
 http://www.digitalmars.com/d/template.html but the solution seem too cumbersome
 for what I'm trying to do, which is to have a few function templates that
 behaves just like their overloaded non-template counterparts. can that be done?
It can't be done in the obvious way... You might try something like this... import std.stdio; void main() { Foo!(int) foo = new Foo!(int)(1,2,3,4,5,6,7); sort(foo); //sort natural ordering sort(foo, (int a, int b) { return -(a - b); } ); //sort reverse order } class Foo (T) { alias T myT; this(T[] args ...) { } } template sortT(T) { void sortT (Foo!(T) foo) { // ... } void sortT (Foo!(T) foo, int delegate(T a, T b) compare) { // ... } void sortT (Foo!(T) foo, int function(T a, T b) compare) { // ... } } void sort(T, X=void*)(T foo, X compare = null) { static assert (is(T : Foo!(T.myT))); static if (!is(X == void*)) { return sortT!(T.myT).sortT(foo, compare); } else { return sortT!(T.myT).sortT(foo); } } ... or a variation of it. The IsExpression can be invaluable at times and I'm sure you can make the above code look nicer. Mine's just a quick hack ;) -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jul 03 2006
parent leoandru <leoandru_member pathlink.com> writes:
In article <e8cejq$85c$1 digitaldaemon.com>, Tom S says...
leoandru wrote:
 correction: I'm using dmd 0.161 not 0.61 as stated.
 
 was reading on template specialization
 http://www.digitalmars.com/d/template.html but the solution seem too cumbersome
 for what I'm trying to do, which is to have a few function templates that
 behaves just like their overloaded non-template counterparts. can that be done?
It can't be done in the obvious way... You might try something like this... import std.stdio; void main() { Foo!(int) foo = new Foo!(int)(1,2,3,4,5,6,7); sort(foo); //sort natural ordering sort(foo, (int a, int b) { return -(a - b); } ); //sort reverse order } class Foo (T) { alias T myT; this(T[] args ...) { } } template sortT(T) { void sortT (Foo!(T) foo) { // ... } void sortT (Foo!(T) foo, int delegate(T a, T b) compare) { // ... } void sortT (Foo!(T) foo, int function(T a, T b) compare) { // ... } } void sort(T, X=void*)(T foo, X compare = null) { static assert (is(T : Foo!(T.myT))); static if (!is(X == void*)) { return sortT!(T.myT).sortT(foo, compare); } else { return sortT!(T.myT).sortT(foo); } } ... or a variation of it. The IsExpression can be invaluable at times and I'm sure you can make the above code look nicer. Mine's just a quick hack ;) -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
cool.. thanks. :) I will work with this. It would be nice if it worked the obvious way though. Thanks again.
Jul 03 2006
prev sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
leoandru escribió:
 Hello fellow D coders,
 
 I'm having a problem and I'm not sure if I'm missing something or trying to do
 something stupid so I hope someone can show me the light here. I have several
 function templates with the same name and different arguments like this
example:
 
 void main()
 {
 Foo!(int) foo = new Foo!(int)(1,2,3,4,5,6,7);
 
 sort!(int)(foo); //sort natural ordering
 sort!(int)(foo, (int a, int b) { return -(a - b); } );  //sort reverse order
 }
 
 class Foo (T)
 {
 this(T[] args ...)
 {
 }
 }
 
 
 void sort(T) (Foo!(T) foo)
 {
 // ...
 }
 
 
 void sort(T) (Foo!(T) foo, int delegate(T a, T b) compare)
 {
 // ...
 }
 
 
 void sort(T) (Foo!(T) foo, int function(T a, T b) compare)
 {
 // ...
 }
 
 The problem im having is when I try to compile something like this I get
 conflicts errors like:
 
 templates.d(24): template templates.sort(T) conflicts with templates.sort(T) at
 templates.d(19)
 templates.d(29): template templates.sort(T) conflicts with templates.sort(T) at
 templates.d(19) 
 
 using dmd 0.61 win xp.
 
 Somehow I though this would have worked in that it would instantiate the
correct
 template based on the functions parameters and the number or parameters given.
 Is there any work around apart from renaming the functions?
 
 Thanks for any assistance.
 
 
I've been trying a couple of things but it seems it can't be done. I don't know if someone else can come up with something. -- Carlos Santander Bernal
Jul 03 2006
parent leoandru <leoandru_member pathlink.com> writes:
In article <e8ce06$7qj$1 digitaldaemon.com>, Carlos Santander says...
leoandru escribió:
 Hello fellow D coders,
 
 I'm having a problem and I'm not sure if I'm missing something or trying to do
 something stupid so I hope someone can show me the light here. I have several
 function templates with the same name and different arguments like this
example:
 
 void main()
 {
 Foo!(int) foo = new Foo!(int)(1,2,3,4,5,6,7);
 
 sort!(int)(foo); //sort natural ordering
 sort!(int)(foo, (int a, int b) { return -(a - b); } );  //sort reverse order
 }
 
 class Foo (T)
 {
 this(T[] args ...)
 {
 }
 }
 
 
 void sort(T) (Foo!(T) foo)
 {
 // ...
 }
 
 
 void sort(T) (Foo!(T) foo, int delegate(T a, T b) compare)
 {
 // ...
 }
 
 
 void sort(T) (Foo!(T) foo, int function(T a, T b) compare)
 {
 // ...
 }
 
 The problem im having is when I try to compile something like this I get
 conflicts errors like:
 
 templates.d(24): template templates.sort(T) conflicts with templates.sort(T) at
 templates.d(19)
 templates.d(29): template templates.sort(T) conflicts with templates.sort(T) at
 templates.d(19) 
 
 using dmd 0.61 win xp.
 
 Somehow I though this would have worked in that it would instantiate the
correct
 template based on the functions parameters and the number or parameters given.
 Is there any work around apart from renaming the functions?
 
 Thanks for any assistance.
 
 
I've been trying a couple of things but it seems it can't be done. I don't know if someone else can come up with something. -- Carlos Santander Bernal
Thanks for trying. I'm really hope this can be done if not now probably in the near future, cause this would be a real set back for me at the moment. Also I'm not liking my work around of using one fuction with default values for traling parameters.
Jul 03 2006