Binding rvalues to const ref in D
Chris Wright via Digitalmars-d
digitalmars-d at puremagic.com
Wed Oct 19 08:58:23 PDT 2016
On Wed, 19 Oct 2016 15:18:36 +0000, Atila Neves wrote:
> The situation is this: if one wants move semantics, one must know when
> one can move. Because rvalues bind to const& in C++, you never know
> whether the const& is an lvalue or rvalue.
To clarify:
You copy lvalues instead of moving them. You move rvalues instead of
copying them. This has an impact when you assign or pass a ref struct
variable to a non-ref variable.
As a practical matter, whenever I come up against a case where I need to
pass something by reference but it's an rvalue, I assign a temporary
variable (after scratching my head at an inscrutable message because,
hey, everyone's using templates right now and the error message just
tells me that I can't pass a DateTime to something expecting a ref T).
So it seems like the compiler could take care of this by only providing
lvalue references but automatically creating those temporary variables
for me. It's going to create an extra copy that might not be needed if
there were a special rvalue reference type modifier, but why should I
care? It's exactly as efficient as the code the compiler forces me to
write.
This is what Ethan Watson has suggested, too.
More information about the Digitalmars-d
mailing list