scope classes on heap

Silvio Ricardo Cordeiro s_r_c_ at hotmail.com
Sun Oct 21 16:22:17 PDT 2007


test2() instantiates Bar on the heap.
Should it be allowed to do so? Bar is a scope class...

scope class Bar {
    long[10000] lng;
    ~this(){
        writefln("dtor! ");
    }
    delete(void* mem){
        writefln("del (heap)!");
    }
    new(uint size){
        writef("new (heap)! ");
        return cast(void*)malloc(size);
    }
}
void test(){
    std.gc.disable();
    while(true){
        test2();
    }
}
void test2(){
    //scope Bar b = new Bar(); // OK: Bar "explicitly" on stack
    //foo(b); // OK: Bar is on stack... deleted when function returns
    //Bar b = new Bar(); // ERROR: Bar "explicitly" on heap
    foo(new Bar()); // But... OK??? Bar "implicitly" on heap
}
void foo(Bar bar){
    writefln("scope bar: ", cast(Bar*)bar);
}


More information about the Digitalmars-d-bugs mailing list