On Thursday, 24 January 2013 at 20:06:34 UTC, Ali Çehreli wrote:
On 01/24/2013 11:33 AM, Jonathan M Davis wrote:

> It's intended. constness matters more than refness when
selecting a function
> overload. From the docs (
http://dlang.org/function.html#<u>function</u>-
> overloading ):
>
> -----
> Functions are overloaded based on how well the arguments to a
function
> can match up with the parameters. The function with the best
match is se
> lected. The levels of matching are:
>
> no match
> match with implicit conversions
> match with conversion to const
> exact match
> -----

That doesn't explain why the first case selects the ref function:

void foo(A a) {
    writeln("without ref");
    foo(a);                    // <-- why is this foo(ref A)?
}

void foo(ref A a) {
    writeln("with ref");
}

foo(A) is the exact match there because the type of a is A.

What is also considered in function selection is the rvalue-lvalue distinction, which shouldn't affect the outcome here either.

Ali

From http://dlang.org/function.html

"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 neither function is more specialized than the other, then it is an ambiguity error. Partial ordering is determined for functions f() and g() by taking the parameter types of f(), constructing a list of arguments by taking the default values of those types, and attempting to match them against g(). If it succeeds, then g() is at least as specialized as f()."

It is possible to pass an lvalue to both ref and non-ref parameters, however rvalue cannot be passed to ref-parameter. Hence ref version for lvalue is more specialized and selected.

Reply via email to