Binding rvalues to const ref in D

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Wed Oct 19 16:04:05 PDT 2016


On Wed, 19 Oct 2016 21:19:03 +0000, Atila Neves wrote:

> 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?

A variable whose type is a struct and which has the `ref` modifier.

>> 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?

Right. For instance, binding query parameters with mysql-native. The 
thing you're binding is passed by reference and I'm not sure why.


More information about the Digitalmars-d mailing list