const(Rvalue) resolved to different overloads

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 29 14:54:35 PST 2016


I'm working on understanding how different qualifiers of the same type, 
the kinds of indirections that its members may have, and the expressions 
being lvalue versus rvalue affect function overload resolution.

For example, the following program has

- struct S with a member having const indirection

- Two overloads of the same function taking S and immutable(S)

- Two calls made with an Rvalue and a const Rvalue

import std.stdio;

struct S {
     const(int)[] a;
}

void foo(S s) {
     writefln("foo(S) called");
}

void foo(immutable(S) s) {
     writefln("foo(immutable(S)) called");
}

void main() {
     writefln("calling with Rvalue");
     foo(S());

     writefln("calling with const Rvalue");
     foo(const(S)());
}

The peculiar thing is that the const Rvalue case is resolved to the 
immutable(S) overload:

calling with Rvalue
foo(S) called
calling with const Rvalue
foo(immutable(S)) called    <-- ?

Can you explain that behavior?

Well... I already see that for that to happen, the argument must have 
the S.init value (or be constructed explicitly as const(S)(null)). When 
initialized with something else, it is resolved to the foo(S) overload:

     const(int)[] a = [ 42 ];
     foo(const(S)(a));

calling with const Rvalue
foo(S) called               <-- Now different; surprising

Bug or not? If not, then I don't think there can ever be more than a 
total of about 0.75 people who fully know D overload resolution. :o)

Ali


More information about the Digitalmars-d mailing list