There is the following piece of code
#include <iostream>
#include <vector>
class A
{
public:
auto foo()
{
//std::vector<int> vec;
return std::vector<int>{} ;
}
};
auto bar()
{
return A();
}
int main() {
for(auto && el : bar().foo())
{
}
}
In my point of view the code above has UB due to dangling reference? Is this correct? I have the impression that in the code above the follwing statement applies
In general, the lifetime of a temporary cannot be further extended by "passing it on": a second reference, initialized from the reference variable or data member to which the temporary was bound, does not affect its lifetime.
However the sanitiser does not detect this issue
Moreover cppreference refers the following
If range-expression returns a temporary, its lifetime is extended until the end of the loop, as indicated by binding to the forwarding reference __range. Lifetimes of all temporaries within range-expression are not (until C++23) extended if they would otherwise be destroyed at the end of range-expression (since C++23).
Have a look in the following demo