Please consider this short code example:
#include <iostream>
struct A
{
A() { std::cout << "A() "; }
~A() { std::cout << "~A() "; }
};
struct B { const A &a = A(); };
int main()
{
B x;
std::cout << ". ";
auto y = B();
std::cout << ". ";
auto z = B{};
std::cout << ". ";
}
GCC prints here ( https://gcc.godbolt.org/z/a83bn54qT ):
A() ~A() . A() ~A() . A() . ~A()
meaning that the lifetime of A-object initializing reference in x and y is short, but in z the lifetime is prolonged till the end of the scope.
Could you please explain why it is so from C++ standard point of view?