Why can't rvalues be passed to a 'ref' parameter?

ryuukk_ ryuukk.dev at gmail.com
Sun Dec 11 17:52:16 UTC 2022


On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear 
wrote:
> Hello,
>
> I am not really understanding why rvalues cannot be passed to a 
> 'ref' parameter, the explanation in the book about D I am 
> reading was not clear:
>
> "The main reason for this limitation is the fact that a 
> function taking a ref
> parameter can hold on to that reference for later use, at a 
> time when the rvalue
> would not be available."
>
> I didn't really understand what Ali meant by this statement, 
> any help would be appreciated so this can be clarified.
>
> Regards,
> thebluepandabear


You can if you compile with: ``-preview=rvaluerefparam``

I personally think this preview should be enabled by default

I use it all the time


```D
struct vec2{ float x; float y;}

void pass(const ref vec2 pos)
{}

void main()
{
     pass( vec2(1,1) );
}
```


If you use dub:

```json
            "dflags": [
                 "-preview=rvaluerefparam",
             ],
```



More information about the Digitalmars-d-learn mailing list