Scope of temporaries as function arguments

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu Jun 27 21:54:48 PDT 2013


Probably a silly question, but I wanted to double-check...

If you have this:

    struct Foo {...}
    bar(Foo());

Then regardless of optimizations (aside from any optimizer bugs, of
course) the Foo temporary can't go out of scope or have its dtor called
until bar finishes executing, right?

Or I guess more accurately, is there any guarantee that the assert in
func() below should always pass?:

    class Foo {
        int i = 1;
        //...etc...
    }

    struct Bar {
        Foo foo;
        ~this() {
            foo.i = 2;
        }
        //...etc...
    }

    void func(Bar bar)
    {
        //...anything here that *doesn't* change bar.foo.i...

        assert(bar.foo.i == 1);  // Guaranteed to pass?
    }

    void main() {
        Foo f = new Foo();
        func(Bar(f));
    }



More information about the Digitalmars-d-learn mailing list