Value based overload resolution?

kdevel kdevel at vogtner.de
Mon Nov 9 22:04:55 UTC 2020


Today I came across this:

~~~id.d
import std.stdio : writeln;

T foo (T) (T s)
{
    __PRETTY_FUNCTION__.writeln;
    return s;
}

short foo (short s)
{
    __PRETTY_FUNCTION__.writeln;
    return s;
}

T id (T) (T t)
{
    return t;
}

int main ()
{
    foo (1);
    foo (1L);
    foo (id (1));
    foo (id (1L));
    foo (0x1_0000);
    foo (0x1_0000L);
    return 0;
}
~~~

Output:

$ ./id
short id.foo(short s)
short id.foo(short s)
int id.foo!int.foo(int s)
long id.foo!long.foo(long s)
int id.foo!int.foo(int s)
long id.foo!long.foo(long s)

It appears to me that the overload resolution may depend on the 
/value/ of the function argument. According to [1] the type of 1 
is int and that of 1L is long. Thus I would have expected foo!int 
and foo!long being called in those cases.

[1] https://dlang.org/spec/lex.html#integerliteral


More information about the Digitalmars-d-learn mailing list