[dmd-beta] rvalue references

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 16 11:43:16 PDT 2012


On Monday, April 16, 2012 11:31:19 Sean Kelly wrote:
> On Apr 16, 2012, at 6:32 AM, Steve Schveighoffer wrote:
> > Regardless of the solution, I think we need to designate two different
> > situations:
> > 
> > 1. I want to pass this by reference because it's more efficient
> > 2. I want to pass this by reference because I want to change it
> 
> Ideally, no one should ever specify "ref" simply for efficiency. The
> programmer should choose semantics for logical reasons, and leave
> optimization up to the compiler. That may still not be practical however.

In many cases, the compiler can't know when a copy is actually required. If 
there's a way for the programmer to indicate that they don't care whether a 
copy is made or not, letting the compiler decide, then that solves the problem 
(which was the idea behind auto ref). Without that though, the compiler will 
have to assume that a copy has to be made in cases where it doesn't. And 
actually, since without auto ref, there's no way to overload a function such 
that the compiler chooses ref or non-ref based on what it thinks is the most 
efficient, I don't see how the compiler could elide copies except in cases where 
inlining eliminates the function call entirely. We need a way for the 
programmer to be able to tell the compiler that they don't care whether the 
argument is passed by ref or not. Barring that, making it possible for const 
ref (and possibly ref) to take rvalues is probably the best that we can do.

- Jonathan M Davis


More information about the dmd-beta mailing list