Given the code below, it will give a warning:
warning: a temporary bound to 'Foo::b' only persists until the constructor exits [-Wextra]
struct Bar {
inline void print() const { std::cout << "Bar" << std::endl; }
};
struct Foo {
Foo() : b{} {} // create temporary
void print() const { b.print(); } // OK but I'm expecting a crash here
Bar&& b;
};
Foo f; // Expecting lifetime of temporary ends here
f.print(); // OK - prints "Bar"
I am expecting accessing Bar::print()
via Foo::b
will lead to crash after Foo
's constructor exits.
How come the program still calls Bar::print()
without a crash?