Function Templates and Overloading
leoandru
leoandru_member at pathlink.com
Mon Jul 3 18:26:02 PDT 2006
In article <e8ce06$7qj$1 at 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.
More information about the Digitalmars-d-learn
mailing list