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?