On Sunday, 14 January 2018 at 01:38:17 UTC, Nordlöw wrote:
My current proposal for a solution is to make `byElement` a
free unary function
byElement(auto ref X x)
which statically checks via
static if (__traits(isRef, x))
whether the `X`-instance is passed as either an
- l-value, where my current solution works (ByLvalueElement), or
- r-value, in which the X-instance instead is moved into range
(ByRvalueElement).
That sounds reasonable. For something like `foreach (e;
makeRange().wrapRangeByRef())`, referencing the makeRange()
struct rvalue by pointer in the wrapped range won't work, as the
underlying range lifetime ends with the foreach range expression,
while the wrapped range's lifetime extends to the end of the
foreach loop. So making the wrapped range take ownership of a
range rvalue by moving it into a member (of ByRvalueElement)
makes sense IMO.
Lvalues aren't moved implicitly by the compiler, so referencing
them by pointer (&this) is safe.