const ref rvalues

Jason House jason.james.house at gmail.com
Mon Dec 14 11:46:22 PST 2009


dsimcha Wrote:

> I mentioned this deep in another thread, but I think it deserves its own
> thread.  Can we get something like:
> 
> void doStuff(T)(const ref T val) {
>     // do stuff.
> }
> 
> T getVal() {
>     return someValue;
> }
> 
> void main() {
>     doStuff(getVal());  // Doesn't currently work.
> }
> 
> For non-const ref parameters, it's understandable that passing rvalues in
> doesn't work because this is likely a bug that the compiler should catch.
> However, for const ref parameters, can't the compiler just implicitly put the
> value on the caller's stack frame and convert it to an lvalue rather than
> forcing the programmer to write the boilerplate to do this manually?  This
> would result in:
> 
> doStuff(getVal()); -->
> 
> auto __temp = getVal();
> doStuff(__temp);

If T is a class, I agree it should work. If T is a struct, you'll need a stronger argument. Maybe scope const ref T would make sense. IMHO, non- scope ref T for structs is already a questionable practice, and I don't mind the compiler making that scenario tougher for me. 



More information about the Digitalmars-d mailing list