forward tuple arg to local variable + dtor

vit vit at vit.vit
Sat Jan 22 15:17:57 UTC 2022


On Saturday, 22 January 2022 at 14:23:32 UTC, Adam Ruppe wrote:
> You can't forward to a local variable. Local variables will be 
> a copy of the tuple. forward only actually works if sent 
> *directly* to another function call.
>
> There's a bunch of things in D that only work in function 
> parameter lists and not local variables. This is one of them.

Thanks,
Why local variable of type tuple call destructors immediately 
after initialization?

```d
import std.stdio : writeln;
import std.meta : AliasSeq;

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 main(){
     {
     	AliasSeq!(Foo, Foo) tup;

         static foreach(alias x; tup)
             writeln("check(", x.i, "): ", cast(void*)&x);	//x is 
destructed
     }
}

```

Print:
```
dtor(0): 7FFF30D76868
dtor(0): 7FFF30D76858
check(0): 7FFF30D76858     //dangling?
check(0): 7FFF30D76868     //dangling?
```



More information about the Digitalmars-d-learn mailing list