On Monday, 6 May 2013 at 14:05:48 UTC, Andrei Alexandrescu wrote:
template <class T> const T& min(const T& a, const T& b) {
    return b < a ? b : a;
}
...
int x = ...;
auto & weird = min(x, 100);


What I see going on is an attempt to double up on the use of ref for twp conflicting purposes. Perhaps part of the solution is to use a new variation of ref that allows rvalues and lvalues, while normal ref continues to disallow rvalues, eg ref vs refr.


void foo(ref b)
{
...
}

ref T min(ref T a, refr T b) {
     ++b; // error refr cannot be modified
     foo(b); // error, cannot pass refr to normal ref
     return b < a ? b : a; // error cannot return refr
 }

The "auto ref" system can then be extended to determine if normal ref or refr is required, and refuse to compile when the rules are violated rather than try and fake a real ref with a temporary, since I would think that's something you'd normally never want done anyway.

A runtime safety check will still be needed for returns of normal ref that may escape.

I can definitely agree on the runtime safety check, but I have doubts about the idea of faking a real ref.

--rt

Reply via email to