structs are now lvalues - what is with "auto ref"?

Jonathan M Davis jmdavisProg at gmx.com
Sun Dec 23 15:59:03 PST 2012


On Monday, December 24, 2012 00:48:01 Namespace wrote:
> As the title says: In dmd 2.061 structs are lvalues now, but we
> have still no auto ref for none template functions. What should I
> do if I need something like that and don't want to write a ref
> and a none-ref function?
> I'm very afraid that "auto ref" will also not work in 2.062 So
> what is the stage of affairs? Or is there any hope that it will
> be fixed in 2.061?

If you have a templated function, you can use auto ref. If you have a non-
templated function, you need to write both a ref and non-ref function (though 
the non-ref version can forward to the ref one avoiding having to duplicate 
the entire function). It's a long-standing issue that has yet to be resolved, 
and it won't be fixed in 2.061 as we're about to start preparing for that 
release.

I think that Kenji was working on trying to get auto ref to work with non-
templated functions in some manner at one point but ran into issues with it, 
and it hasn't really been decided what the correct solution is, so I don't 
know when it will be fixed. Plenty of folks would simply argue for const ref 
working like it does in C++, but Andrei is dead set against that, so it hasn't 
happened. It's not necessarily a good fix anyway though given the fact that 
const is so much more restrictive in D. auto ref will accept both lvalues and 
rvalues but will allow for the mutation of the argument, making it somewhat 
different from what you get with const& in C++, but it also avoids having to 
restrict yourself with const (and you can still use const with auto ref if you 
want to). So, we may need a similar fix with non-templated functions.

As for struct literals, it never made sense for them to be lvalues. They're 
temporaries, not variables. And it causes stupidity like

S foo() {...}
auto bar(ref S) {...}

bar(S()); //compiles
bar(foo()); //doesn't compile

There's no question though that the issue with const ref / auto ref needs to 
be resolved. But unfortunately, that's still an open issue.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list