[dmd-beta] D 2.059 beta 4

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 9 23:38:47 PDT 2012


On Monday, April 09, 2012 23:28:27 Walter Bright wrote:
> On 4/9/2012 10:51 PM, Jonathan M Davis wrote:
> > 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.
> 
> Let's say the compiler auto-generates the following:
> 
>     void foo(T t) { foo(t); }
> 
> in case it sees:
> 
>      void foo(ref T t) { ... }
> 
> I don't think that's a solution at all. It's just a roundabout way for the
> compiler to generate a temporary anyway. Nothing has been gained.

It wouldn't be a good idea for every single ref to end up with a duplicated 
function like that, since if ref is what was actually intended, then the code 
would behave incorrectly. With auto ref, at least the programmer is signalling 
that that's what they mean. On the whole, I don't think that it's all that big 
a deal with ref, since when you use ref, you really want a ref. It's const ref 
that's the problem.

As it stands, if you use const ref, you have to create an extra function or be 
forced to create extraneous variables just to call the function, and that's 
not at all friendly. If auto ref actually did what it was intended to do and 
made it so that you could pass by either ref or non-ref essentially like what 
occurs with C++'s const&, then you wouldn't have to be duplicating functions, 
and it would be _much_ more programmer-friendly. Sure, it would be nice if 
that could be done without the compiler duplicating the function, but having

void foo(auto ref T t) {...}

become

void foo(T t) { foo(t); }
void foo(ref T t) { ... }

and have

void foo(const auto ref T t) {...}

become

void foo(const T t) { foo(t); }
void foo(const ref T t) {...}

or maybe even have

void foo(auto ref T t) {...}

become

void foo(const T t) { foo(t); }
void foo(const ref T t) {...}

then it would be a big step forward, because you wouldn't have to be 
duplicating pretty much every const ref function like you do no. Even if it's 
just a "roundabout way for the compiler to generate a temporary," it's still a 
huge boon to usability. It would be nice if there were another way to do it 
without having the compiler duplicate the function like that, but lacking a 
proposal which would make auto ref work in a better way, I think that it's the 
way to go.

- Jonathan M Davis


More information about the dmd-beta mailing list