2

If I have a setup where a function takes an object Bar as an argument, passes that object to a local class Foo, and Foo uses the Bar in its destructor such as:

class Foo {
    public:
        Foo(const Bar& bar) : bar_(bar) {}
        ~Foo() {
            bar_.DoSomething();
        }
    private:
        const Bar& bar_;
};

void example_fn(const Bar& input_bar) {
    Foo local_foo(input_bar);
    // ... do stuff.
    // When foo goes out of scope, its destructor is called, using input_bar.
}

If example_fn is called with a temporary Bar input_bar, is the local variable Foo local_foo guaranteed to be destroyed before the temporary argument? In other words, are arguments guaranteed to outlive local variables?

mfsiega
  • 2,852
  • 19
  • 22
  • `example_fn` can't really be called with a temporary, since mutable lvalue references don't bind to temporaries. – Kerrek SB Jul 12 '16 at 16:47
  • "is the local variable Foo local_foo guaranteed to be destroyed before the temporary argument" - `input_bar` you mean? It is not a temporary... – WhiZTiM Jul 12 '16 at 16:47
  • 1
    As far as `example_fn` is concerned, the object referenced by `input_bar` isn't temporary, so I'm not sure what you're asking. Further, `Bar bar` the constructor parameter is *copied* from `input_bar`, so `input_bar` becomes even less important after construction of `local_foo`. In short, your comment "When `foo` goes out of scope, its destructor is called, using `input_bar`." isn't true. – WhozCraig Jul 12 '16 at 16:48
  • related/dupe: http://stackoverflow.com/questions/36992569/what-is-the-order-of-destruction-of-function-parameters – NathanOliver Jul 12 '16 at 16:53
  • Sorry, left out some relevant modifiers - does this example make more sense? – mfsiega Jul 12 '16 at 16:54
  • Also note that `Foo(const Bar& bar) : bar_(bar) {}` is not going to extend the lifetime of the variable passed into the constructor. The `const &` extending lifetime only applies to local function objects. not actually the case here but just as an FYI. – NathanOliver Jul 12 '16 at 17:31

1 Answers1

7

is the local variable Foo local_foo guaranteed to be destroyed before the temporary argument?

Yes, Objects with automatic storage duration (aka locals) are guaranteed to be destroyed in the reverse order of construction. The function arguments are always constructed (in an unspecified order) before the locals within the block scope. See Object Destruction in C++

[class.temporary/5]

5: ...In addition, the destruction of temporaries bound to references shall take into account the ordering of destruction of objects with static, thread, or automatic storage duration ([basic.stc.static], [basic.stc.thread], [basic.stc.auto]); that is, if obj1 is an object with the same storage duration as the temporary and created before the temporary is created the temporary shall be destroyed before obj1 is destroyed; if obj2 is an object with the same storage duration as the temporary and created after the temporary is created the temporary shall be destroyed after obj2 is destroyed. ...

Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68