Binding rvalues to const ref in D
Atila Neves via Digitalmars-d
digitalmars-d at puremagic.com
Wed Oct 19 14:19:03 PDT 2016
On Wednesday, 19 October 2016 at 15:58:23 UTC, Chris Wright wrote:
> 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.
Then there's this:
void foo(ref Foo); //doesn't copy lvalues
void foo(Foo);
What's a ref struct 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).
I'm assuming this happens because you don't control the signature
of the function you're calling and it takes by ref?
> 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.
Interesting. Also, I must have missed that suggestion.
Atila
More information about the Digitalmars-d
mailing list