meaning of "auto ref const"?

Picaud Vincent via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 20 13:27:21 PST 2016


On Tuesday, 20 December 2016 at 19:24:32 UTC, Ali Çehreli wrote:
> As a general rule, 'auto ref' should probably be const. If the 
> purpose of 'ref' is so that the argument would be mutated, then 
> allowing a copy of an rvalue to this function could very well 
> be a bug:
>
> struct S {
>     int i;
> }
>
> void foo()(auto ref S s) {
>     s.i = 42;  // <-- Cannot be observed if the arg is rvalue
> }
>
> void main() {
>     foo(S(1));
> }

Thank you Ali! This is effectively a trap I had not realized, you 
probably save me from some long debugging time.


More information about the Digitalmars-d-learn mailing list