[dmd-beta] D 2.059 beta 4

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 9 22:51:02 PDT 2012


On Tuesday, April 10, 2012 00:39:26 Michel Fortin wrote:
> Le 2012-04-10 à 0:06, Jonathan M Davis a écrit :
> > Having to duplicate pretty much every single function which takes a const
> > ref - opEquals and opCmp being very prominent among them - is a definite
> > problem. That's the problem that auto ref was intended to solve, and as
> > long as it only works with templated functions, it really doesn't solve
> > the problem.
> I think the implementation of auto-ref you want is one where the argument is
> always passed by ref but it the function would also accept lvalues. I think
> "auto ref" is a misnomer for that.
> 
> Actually, I think the true problem you are trying to solve using auto-ref is
> one of efficiency: you don't want all structs to be passed by copy because
> often that'd be inefficient. Disregarding efficiency, all cases where you
> want to use auto-ref you could pass the struct by copy instead. Am I right?

Yes, assuming that you're not dealing with a struct which isn't copyable. In 
C++, you use const& for objects to avoided unnecessary duplication. But that 
doesn't work in D, since const ref won't take rvalues. So, you're forced to 
duplicate the function. Also, some functions have historically required const 
ref (e.g. opEquals), but I don't know if they do now or not.

The whole point of auto ref is to have the compiler take the argument in the 
most efficient manner - be it by ref or by value; you don't care which. But it 
only works with templated functions right now, so it doesn't really solve the 
problem.

I really don't care how it's implemented for not-templated functions. It can 
be the outright equivalent of C++'s const& for all I care. But without auto 
ref on non-templated functions, we're forced to duplicate any non-templated 
function which takes const ref.

- Jonathan M Davis


More information about the dmd-beta mailing list