Const ref and rvalues again...

martin kinke at libero.it
Tue Nov 6 15:37:23 PST 2012


On Tuesday, 6 November 2012 at 22:32:57 UTC, Manu wrote:
> But it only really makes sense in the context of templates...?
> Why should something called 'auto ref' provide fabrication of 
> temporaries
> for the purpose of passing rvalues to functions that receive 
> ref args?
>
> How does auto ref (under some different 
> definition/implementation) address
> the rvalue problem?

The thing is that currently there are 2 workarounds. You 
described the first one - allocating temporaries manually to 
obtain referenceable lvalues:

void func(ref in Vector m);
Vector v1v2PlusSomeStuff = v1*v2 + Vector(10, 20, 30);
func(v1v2PlusSomeStuff);

The other, also painfully annoying workaround is overloading func:

void func(in ref Vector m);
void func(in Vector m);
func(v1*v2 + Vector(10, 20, 30));

'auto ref' implements the second workaround (via a template) and 
therefore requires a single, but templated func() implementation:

void func(T)(in auto ref T m);

This template, as I understand it, gets expanded to:

void func(T)(in ref T m); // for lvalues
void func(T)(in T m);     // for rvalues

So for non-templated functions, I suggest 2 options:

1) The previously described auto-templates (identical 'auto ref' 
semantics), where a function with 'auto ref' parameters is 
treated as implicit template. This may lead to code-bloating (for 
larger functions) and/or higher performance for rvalue arguments 
(rvalues passed to value arguments are moved, not copied; we 
therefore gain nothing by passing a reference, but incur a slight 
performance hit due to pointer indirection instead of accessing 
directly the rvalue on the stack). OR
2) Simple under-the-hood temporary lvalue declaration by the 
compiler (for rvalues passed to 'const ref' parameters) - that 
would be a handy implementation of the first workaround.

I hope you get my point. :)


More information about the Digitalmars-d mailing list