digitalmars.D.learn - Struct dtor on ref variable
- Patric (38/38) Aug 01 2016 struct Test{
- Steven Schveighoffer (4/6) Aug 01 2016 You want opAssign, not opOpAssign. opOpAssign is for things like +=.
- Patric (6/15) Aug 01 2016 Same thing:
- Mike Parker (3/22) Aug 01 2016 You've implemented opAssign incorrectly. See:
- Patric (3/12) Aug 01 2016 Sorry, silly me, forgot to remove the "string op" now its ok :)
- Mike Parker (2/18) Aug 01 2016 Well, you beat me to it :)
- Patric (5/14) Aug 01 2016 But still.
- Mike Parker (3/6) Aug 01 2016 No. It was working as expected. You never implemented opAssign,
- Patric (2/8) Aug 01 2016 Iep, I realized this by now. Thanks anyway :)
struct Test{
int x;
this(int v){
x = v;
writeln(x.to!string ~ " Created");
}
~this(){
writeln(x.to!string ~ " Destroyed");
}
void opOpAssign(string op, Type)(ref Type s){
x = s.x;
}
}
void showme(Type)(ref Type t){
writeln(t.x);
}
void main(){
auto t = Test(1);
auto t2 = Test(2);
showme(t);
showme(t2);
t = t2;
}
Prints:
1 Created
2 Created
1
2
1 Destroyed
2 Destroyed
2 Destroyed
this line:
t = t2
Causes the dtor to be called.
Why?
I expected nothing to happen because "ref" its a simple pointer,
right?
Or I am missing something here?
Aug 01 2016
On 8/1/16 12:01 PM, Patric wrote:I expected nothing to happen because "ref" its a simple pointer, right? Or I am missing something here?You want opAssign, not opOpAssign. opOpAssign is for things like +=. Your code had me worried for a while :) -Steve
Aug 01 2016
On Monday, 1 August 2016 at 16:05:51 UTC, Steven Schveighoffer wrote:On 8/1/16 12:01 PM, Patric wrote:Same thing: (Remembered now of DPaste) (and I understand your concern :P) https://dpaste.dzfl.pl/af512b5f6288I expected nothing to happen because "ref" its a simple pointer, right? Or I am missing something here?You want opAssign, not opOpAssign. opOpAssign is for things like +=. Your code had me worried for a while :) -Steve
Aug 01 2016
On Monday, 1 August 2016 at 16:14:31 UTC, Patric wrote:On Monday, 1 August 2016 at 16:05:51 UTC, Steven Schveighoffer wrote:You've implemented opAssign incorrectly. See: https://dlang.org/spec/operatoroverloading.html#assignmentOn 8/1/16 12:01 PM, Patric wrote:Same thing: (Remembered now of DPaste) (and I understand your concern :P) https://dpaste.dzfl.pl/af512b5f6288I expected nothing to happen because "ref" its a simple pointer, right? Or I am missing something here?You want opAssign, not opOpAssign. opOpAssign is for things like +=. Your code had me worried for a while :) -Steve
Aug 01 2016
On Monday, 1 August 2016 at 16:05:51 UTC, Steven Schveighoffer wrote:On 8/1/16 12:01 PM, Patric wrote:Sorry, silly me, forgot to remove the "string op" now its ok :)I expected nothing to happen because "ref" its a simple pointer, right? Or I am missing something here?You want opAssign, not opOpAssign. opOpAssign is for things like +=. Your code had me worried for a while :) -Steve
Aug 01 2016
On Monday, 1 August 2016 at 16:17:02 UTC, Patric wrote:On Monday, 1 August 2016 at 16:05:51 UTC, Steven Schveighoffer wrote:Well, you beat me to it :)On 8/1/16 12:01 PM, Patric wrote:Sorry, silly me, forgot to remove the "string op" now its ok :)I expected nothing to happen because "ref" its a simple pointer, right? Or I am missing something here?You want opAssign, not opOpAssign. opOpAssign is for things like +=. Your code had me worried for a while :) -Steve
Aug 01 2016
On Monday, 1 August 2016 at 16:05:51 UTC, Steven Schveighoffer wrote:On 8/1/16 12:01 PM, Patric wrote:But still. If it was the case of "+=" wasn´t wrong to call the dtor since is a ref var?I expected nothing to happen because "ref" its a simple pointer, right? Or I am missing something here?You want opAssign, not opOpAssign. opOpAssign is for things like +=. Your code had me worried for a while :) -Steve
Aug 01 2016
On Monday, 1 August 2016 at 16:18:32 UTC, Patric wrote:But still. If it was the case of "+=" wasn´t wrong to call the dtor since is a ref var?No. It was working as expected. You never implemented opAssign, so default assignment was being used. There was no ref variable.
Aug 01 2016
On Monday, 1 August 2016 at 16:21:16 UTC, Mike Parker wrote:On Monday, 1 August 2016 at 16:18:32 UTC, Patric wrote:Iep, I realized this by now. Thanks anyway :)But still. If it was the case of "+=" wasn´t wrong to call the dtor since is a ref var?No. It was working as expected. You never implemented opAssign, so default assignment was being used. There was no ref variable.
Aug 01 2016









Mike Parker <aldacron gmail.com> 