Thread overview
rvalue based copy
Mar 30, 2015
matovitch
Mar 30, 2015
matovitch
Mar 30, 2015
matovitch
Mar 30, 2015
Adam D. Ruppe
Mar 30, 2015
matovitch
Mar 30, 2015
Adam D. Ruppe
Mar 30, 2015
anonymous
Mar 30, 2015
matovitch
March 30, 2015
Hi,

Surely I am misunderstanding something.

I got something like this :

struct S
{
    void opAssign(const ref s)
    {
        //...
    }
}

S genS()
{
    S s;
    //...
    return s;
}

main()
{
    S s;
    s = genS();
}

DMD says : ...opAssign (ref const(S) point) is not callable using argument types (S).

Then how to do what I wanna do ? Why doesn't this works ? (I am gessing ref argument explitly means no rvalue)

Thanks in advance for your help ! :)
March 30, 2015
The title should be assignement not copy.
March 30, 2015
On Monday, 30 March 2015 at 17:09:14 UTC, matovitch wrote:
>(I am gessing ref argument explitly means no rvalue)

That's right. I'd first say don't use ref, just use "const S" and it will work and probably do what you need efficiently.

If you do want it to be ref though, rvalues aren't allowed unless you make it "auto ref" which needs to be a template:

// this will work, second set of () makes it a template
// then auto ref makes it use ref for lvalues and non-ref for rvalues
// automatially
    void opAssign()(const auto ref S s)
    {
        //...
    }
March 30, 2015
void opAssign(const ref s) should be void opAssign(const ref S s) btw and btw bis, I should probably make it const ref SopAssign(const ref S s)  :/ I stop flooding there.
March 30, 2015
On Monday, 30 March 2015 at 17:14:27 UTC, Adam D. Ruppe wrote:
> On Monday, 30 March 2015 at 17:09:14 UTC, matovitch wrote:
>>(I am gessing ref argument explitly means no rvalue)
>
> That's right. I'd first say don't use ref, just use "const S" and it will work and probably do what you need efficiently.

Yes but you know what they say does it really do a copy of the struct or is the compiler smart enougth most of the time to avoid copy. (I think it's called return value optimization).
>
> If you do want it to be ref though, rvalues aren't allowed unless you make it "auto ref" which needs to be a template:
>
> // this will work, second set of () makes it a template
> // then auto ref makes it use ref for lvalues and non-ref for rvalues
> // automatially
>     void opAssign()(const auto ref S s)
>     {
>         //...
>     }

Why is this only restricted to templates ?
March 30, 2015
On 3/30/15 1:09 PM, matovitch wrote:
> Hi,
>
> Surely I am misunderstanding something.
>
> I got something like this :
>
> struct S
> {
>      void opAssign(const ref s)
>      {
>          //...
>      }
> }
>
> S genS()
> {
>      S s;
>      //...
>      return s;
> }
>
> main()
> {
>      S s;
>      s = genS();
> }
>
> DMD says : ...opAssign (ref const(S) point) is not callable using
> argument types (S).
>
> Then how to do what I wanna do ? Why doesn't this works ? (I am gessing
> ref argument explitly means no rvalue)
>
> Thanks in advance for your help ! :)

One solution is to overload

void opAssign(ref const S s) {...}
void opAssign(const S s) {...}

lvalues will go into the ref version, rvalues into the non-ref. There won't be any copying of data, so you still save a postblit and copying on the stack.

But you have to repeat the implementation.

Another possibility is to use auto ref, but that requires a template. Annoying as this is (and blatantly awkward), it saves you from having to implement twice:

void opAssign(T)(auto ref const T s) if(is(T == S)) {...}

-Steve
March 30, 2015
On Monday, 30 March 2015 at 17:20:30 UTC, matovitch wrote:
> Yes but you know what they say does it really do a copy of the struct or is the compiler smart enougth most of the time to avoid copy. (I think it's called return value optimization).

Copying isn't necessarily a problem, for small structs it is more efficient to copy than passing by ref. But the return value optimization *is* typically done, yes.

> Why is this only restricted to templates ?

It makes two versions of the function, like overloading on the two types of arguments automatically.
March 30, 2015
On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote:
> One solution is to overload
>
> void opAssign(ref const S s) {...}
> void opAssign(const S s) {...}
>
> lvalues will go into the ref version, rvalues into the non-ref. There won't be any copying of data, so you still save a postblit and copying on the stack.
>
> But you have to repeat the implementation.

You can call the ref version from the non-ref version:

void opAssign(ref const S s) {...}
void opAssign(const S s) {opAssign(s); /* calls the ref version */}

Of course, only do this when the ref version doesn't store &s.
March 30, 2015
On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote:
 Annoying as this is (and blatantly awkward), it saves
> you from having to implement twice:
>
> void opAssign(T)(auto ref const T s) if(is(T == S)) {...}

Yep, this seems awkward to me too thought according to Adam one can do :

void opAssign()(auto ref const S s) {...}
March 30, 2015
On 3/30/15 1:42 PM, matovitch wrote:
> On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote:
>   Annoying as this is (and blatantly awkward), it saves
>> you from having to implement twice:
>>
>> void opAssign(T)(auto ref const T s) if(is(T == S)) {...}
>
> Yep, this seems awkward to me too thought according to Adam one can do :
>
> void opAssign()(auto ref const S s) {...}

Yeah, if Adam says it works, it probably does. I thought it didn't, but I think it's only types that don't allow you to omit the compile-time parameters, not functions.

-Steve