overloading functions against function templates

bearophile bearophileHUGS at lycos.com
Thu Jul 30 14:47:13 PDT 2009


Walter Bright:
>Currently, that can't be done. But it would be good to get it in for D2.<

I think someone has already asked for this feature, time ago :-)


> The question is, what rule to use?
> I suggest that:
> 1. if any functions match, then overload functions the usual way
> 2. if no functions match, then overload the function templates the usual way
> Or reverse the priority of the two.
> What do you think?

I'm ignorant, so here is an example to see if I have understood what you mean:
import std.stdio: writeln;
double foo(int x) { return x * 2; }
double foo(float x) { return x * 3; }
double foo(T)(T x) { return x * 4; }
double foo(T : double)(T x) { return x * 5; }
void main() {
    writeln(foo(10));    // prints: 
    writeln(foo(10.5));  // prints: 
    writeln(foo(10.5f)); // prints: 
    writeln(foo(10U));   // prints: 
}

What's the output?
I think functions have to be used first, when possible, to reduce "template bloat".

Bye,
bearophile



More information about the Digitalmars-d mailing list