Dean Dean - 2 days ago 5
C++ Question

Rvalue reference to lvalue reference - ub?

This is not a duplicate of Rvalue reference to lvalue reference

Since I'm asking this for C++17: does the following still invoke undefined behavior (if the reference is used, of course) due to a dangling reference?

template <typename T>
constexpr T& to_lvalue(T&& r) noexcept { return r; }
int main()
{
int ten = 10;
int& ref = to_lvalue(ten+8);
// Is ref a dangling reference in C++17?
}

Answer

Yes, the reference dangles. There was some talk about possibly extending the lifetime-extension system to some additional cases, but AFAIK nothing was actually done for C++17.

Comments