Is the declaration const int& r = 5; valid in C++?
Correct answer B. Yes; a temporary is created and its lifetime is extended to match r
Explanation
A non-const lvalue reference (int&) cannot bind to a literal, but a reference to const can. The compiler materialises a temporary int holding 5 and binds r to it, and binding a reference directly to a temporary extends the temporary's lifetime to that of the reference. This is why functions taking const T& accept literals and other temporaries.
Answer checked How we write and check questions
Discussion