mixin template scope inconsistency?
Timothee Cour via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Mar 3 22:23:22 PST 2015
posted as bugzilla/14243. Am I misunderstanding something here?
On Tue, Mar 3, 2015 at 10:20 PM, Timothee Cour <thelastmammoth at gmail.com>
wrote:
> Template mixin scope seems to have a weird behavior:
> I don't understand 'WEIRD(1)' and 'WEIRD(2)' below.
>
> import std.stdio;
> struct A{
> int b;
> this(int b){
> this.b=b;
> writeln("A.begin");
> }
> ~this(){
> writeln("A.end");
> }
> }
>
> mixin template Entry(){
> auto a=A(12);
> }
>
> void test1(){
> writeln("test.begin");
> mixin Entry;
> writeln(a.b);//WEIRD! destructor has been called but a.b is still alive
> writeln("test.end");
> }
>
> void test2(){
> writeln("test.begin");
> auto a=A(0);
> writeln("test.end");
> }
>
> void test3(){
> writeln("test.begin");
> mixin("auto a=A(0);");
> writeln("test.end");
> }
>
> void main(){
> test1;
> writeln;
> test2;
> writeln;
> test3;
> }
>
> output:
> ----------------
> test.begin
> A.begin
> A.end //WEIRD(1): why is destructor called before test.end?
> 12 //WEIRD(2): destructor has been called but a.b is still alive
> test.end
>
> test.begin
> A.begin
> test.end
> A.end
>
> test.begin
> A.begin
> test.end
> A.end
> ----------------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150303/03fd35e4/attachment.html>
More information about the Digitalmars-d-learn
mailing list