The liabilities of binding rvalues to ref

Dmitry S ds.dlang at gmail.com
Thu May 9 14:13:47 PDT 2013


>
> I'm not sure about how common it is, but I really don't like the idea of
> calls like swap(1, 2) being legal. Seems like a step backward from C++.
>

There are useful analogs, however. E.g. consider

heap.swap(parentIndex, parentIndex * 2 + 1)
heap.swap(getParent(index), index)

It's convenient and intuitive if the lvalues in these calls can be accepted
by reference and modified. And it's not a problem that the modification to
the rvalue is lost -- that's what's desired in this case.

One alternative which I haven't seen (though I haven't read the entire
thread) is to require the caller to add some syntax when passing an rvalue
to a ref parameter. E.g. in the above example

heap.swap(parentIndex, tempRef(parentIndex * 2 + 1))

would look a little clearer, by making it more explicit what's going on
with that second parameter. I imagine "tempRef" would have to be some
language feature, a sort of alternative to "auto ref", but to be used at
the call site.

Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130509/afcf09d6/attachment.html>


More information about the Digitalmars-d mailing list