rvalue references

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 27 17:17:38 PDT 2013


On Saturday, April 27, 2013 13:11:54 Namespace wrote:
> You often say that 'auto ref' can not work for non-templates.

Please quote at least part of the message that you're replying to. The 
threading of the various clients is often wrong, and I have no way of knowing 
who you're talking to here (and some people don't use threading at all). Based 
on the content, I'm guessing that you're talknig to me, but I really don't 
know.

> Why not really?
> Because the resulting code bloat is not bearable, or because it
> is technically not possible?

Because the templated solution involves creating new functions, whereas that's 
not going to work for non-templated functions, particularly when you consider 
.di files and prototypes. It also gets particularly nasty when function 
overriding gets added into the mix. Walter himself has said several times in 
the past that the solution used for auto ref and templates can't work with 
non-templated functions.

> Also I would like to see an answer of my overlooked question
> (just out of curiosity):
> http://forum.dlang.org/thread/kl4v8r$tkc$1@digitalmars.com?page=7#post-kyicm
> dsriwnxqiuzkaho:40forum.dlang.org

So, you're asking why the compiler can't just do a move in the rvalue case and 
pass by ref in the lvalue case for auto ref? The semantics are completely 
different. You need _one_ function, and you can't do that with one function. 
That would be like asking the function to be both

auto foo(int* i) {...}

and

auto foo(int i) {...}

only we're talking about ref rather than pointers (but ref is really a pointer 
underneat the hood). A function can only have one signature unless it's a 
template, and the only way that templates can have more is because they 
generate multiple functions (each with only one signature).

Remember that we're dealing with the C linker here, so we're pretty much 
restricted to what you can ultimately do in C as far as function signatures 
go. We have to do name mangling to do function overloading or add additional 
information to function signatures (such as pure or nothrow), and the only 
information you ultimately have is the function signature, so anything 
requiring messing with how the function works either must be done inside the 
function (where it knows nothing about the caller), or you need multiple 
functions, which generally means templates. To get around all of that, we'd 
need to create our own linker which didn't work like C's linker does, which 
would create its own set of problems.

- Jonathan M Davis


More information about the Digitalmars-d mailing list