forward tuple arg to local variable + dtor

vit vit at vit.vit
Sat Jan 22 14:12:00 UTC 2022


Hello,

Why is tuple variable `params` immediately destructed after its 
construction?
Why is `check(-2)` after `dtor(2)`?

Code:
```d
import std.stdio : writeln;
import core.lifetime : forward, move;

struct Foo{
	int i;

     this(int i){
     	this.i = i;
         writeln("ctor(", i, "): ", cast(void*)&this);
     }

     ~this(){
         writeln("dtor(", i, "): ", cast(void*)&this);
         i *= -1;
     }
}

void seq(Args...)(auto ref Args args){
     Args params = forward!args;
     writeln("params initialized");

     alias foo = params[0];
     writeln("check(", foo.i, "): ", cast(void*)&foo);

}

void main(){
     writeln("Foo(1):");
     {
     	auto foo = Foo(1);
         writeln("check(", foo.i, "): ", cast(void*)&foo);
     }

     writeln("\nFoo(2):");
     seq(Foo(2));
}


```

Output:
```
Foo(1):
ctor(1): 7FFEBCAF0538
check(1): 7FFEBCAF0538
dtor(1): 7FFEBCAF0538

Foo(2):
ctor(2): 7FFEBCAF0548
dtor(2): 7FFEBCAF04F0
params initialized
check(-2): 7FFEBCAF04F0
dtor(0): 7FFEBCAF0558
```




More information about the Digitalmars-d-learn mailing list