Function Templates and Overloading
Carlos Santander
csantander619 at gmail.com
Mon Jul 3 17:47:37 PDT 2006
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
More information about the Digitalmars-d-learn
mailing list