In the following the snippet, is the lifetime of the sphere extended in such a way that the value of r
is not undefined?
struct Sphere {
auto& radius() const { return _radius;}
float _radius{};
};
struct Capsule {
auto sphere() const { return Sphere{12.0}; }
};
auto func() {
auto capsule = Capsule{};
const auto& r = capsule.sphere().radius();
std::cout << r;
}
I know that const-references extend the life time of a temporary, but I'm not sure what happens if a member of a temporary is bound to.
Note: I'm very suspicious that the equivalent of this snippet is causing a bug for me, but neither Clang nor Visual Studio issues a warning.