opAssign overload question
Ali Çehreli
acehreli at yahoo.com
Thu Apr 25 12:14:02 PDT 2013
On 04/25/2013 11:37 AM, gedaiu wrote:> Hi folks,
>
> i have this struct:
Reduced code:
import std.stdio;
void foo(bool b)
{
writeln("bool");
}
void foo(long l)
{
writeln("long");
}
void main()
{
foo(1);
}
The bool overload gets called...
> Can anyone tell me why the compiler call opAssign(bool val) and not
> opAssign(long val). I am passing an long value not a bool one.
The type of literal 1 is int, not long. So, both functions are matched
with implicit conversions. According to "Function Overloading" here:
http://dlang.org/function.html
The match is later resolved by "If two or more functions have the same
match level, then partial ordering is used to try to find the best
match. Partial ordering finds the most specialized function."
If bool is considered to be more specialized than long, then the
compiler is behaving according to spec. It is very confusing indeed.
Especially, considering that an int *variable* would be matched to long:
auto i = 1;
foo(i);
Now the long overload gets called!
Ali
More information about the Digitalmars-d-learn
mailing list