rvalues -> ref (yup... again!)

Manu turkeyman at gmail.com
Sat Mar 24 17:34:09 UTC 2018


On 24 March 2018 at 06:49, Timon Gehr via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On 24.03.2018 05:03, Manu wrote:
>>
>> I have no idea what this paragraph means... can you elaborate further
>> what you're talking about?
>
> This works:
>
> struct S{
>     int x;
>     void inc(){
>         this.x++; // note: 'this' is passed by ref
>     }
> }
>
> void main(){
>     S().inc();
> }
>
> but this does not:
>
> struct S{
>     int x;
> }
> void inc(ref S self){
>     self.x++; // note: 'self' is passed by ref
> }
>
> void main(){
>     S().inc();
> }
>
> I.e. there is a special case where your rewrite is already applied. Note how
> "inc" cannot even be made const.
>
> What I'm saying is that I don't really buy Jonathan's argument. Basically,
> you should just pass the correct arguments to functions, as you always need
> to do. If you cannot use the result of some mutation that you need to use,
> you will probably notice.

Your example demonstrates the exact reason why rvalue->ref is const&
in C++, and illegal in D though.
You mutate a temporary that times out at the end of the statement...
your statement is never assigned to anything, and has no effect.
If your statement is assigned to something, then you already have an
lvalue to pass to such a function that receives mutable ref.


More information about the Digitalmars-d mailing list