meaning of "auto ref const"?

kinke via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 18 06:32:08 PST 2016


On Sunday, 18 December 2016 at 13:14:08 UTC, Picaud Vincent wrote:
> bool opEquals()(auto ref const BigInt y) const pure @nogc
> {
>    return sign == y.sign && y.data == data;
> }
>
> my problem is that I do not understand the role/meaning of 
> "auto" in this context.

See https://dlang.org/spec/template.html#auto-ref-parameters. 
It's used to end up with an `opEquals(ref const BigInt y)` for 
lvalue args (passed by reference) and with an `opEquals(const 
BigInt y)` for rvalue args (passed by value => implicitly moved 
in D (as they are rvalues)).

> Moreover in the opCmp code, "auto" is not present anymore, 
> which is an extra source of confusions for me.
>
> int opCmp(ref const BigInt y) pure nothrow @nogc const
> {
>    // Simply redirect to the "real" opCmp implementation.
>    return this.opCmp!BigInt(y);
> }

TypeInfo_Struct apparently requires (or used to require) an `int 
opCmp(ref const T rhs)` overload, i.e., a version taking the rhs 
lvalue argument by reference (see 
https://dlang.org/spec/operatoroverloading.html#compare). Note 
that there are other overloads afterwards which take the rhs 
argument by value, thereby allowing rhs rvalues too.


More information about the Digitalmars-d-learn mailing list