Interview Questions
References: lvalue and rvalue · Study material
C++ References – Interview Questions
1. What is a reference in C++?
An alias – another name – for an existing object. It must be initialized and always refers to the same object.
2. What are the differences between a reference and a pointer?
References cannot be null, cannot be re-seated, need no dereference syntax and do not support arithmetic; pointers can be null, can point elsewhere, use * and -> and support arithmetic.
3. Can a reference be reassigned?
No. r = y assigns y's value to the object r refers to; it does not make r refer to y.
4. Why can a const reference bind to a temporary but a non-const one cannot?
Modifying a temporary that disappears immediately is almost always a bug, so the language allows only read-only (const&) or rvalue (&&) references to bind to it. Binding a const& directly extends the temporary's lifetime.
5. What is a dangling reference?
A reference to an object whose lifetime has ended, e.g. a returned reference to a local variable. Using it is undefined behaviour.
6. What is an lvalue and an rvalue?
An lvalue designates an object with identity (a named variable, *p, a[i]); an rvalue is a temporary value without identity (literals, x + y, function results returned by value).
7. What is an rvalue reference?
A reference declared with && that binds only to rvalues. It lets functions detect temporaries and move from them.
8. What does std::move do?
It casts its argument to an rvalue reference (static_cast<T&&>). The actual moving happens in the move constructor or move assignment operator chosen as a result.
9. What is the state of an object after it has been moved from?
Valid but unspecified: it can be destroyed or assigned a new value, but its contents should not be relied on.
10. What is a forwarding (universal) reference?
A parameter of the form T&& where T is a deduced template parameter (or auto&&). It binds to lvalues and rvalues.
11. What does std::forward do?
Casts a forwarding reference back to its original value category, so lvalues stay lvalues and rvalues stay rvalues when passed on.
12. Explain reference collapsing.
When references to references arise in templates, & &, & && and && & collapse to &; only && && collapses to &&.
13. Is a named rvalue reference an lvalue or an rvalue?
An lvalue – anything with a name is an lvalue. That is why std::move or std::forward is needed to pass it on as an rvalue.
14. Can you have an array or vector of references?
No, references are not objects. Use pointers or std::reference_wrapper.
15. What is std::reference_wrapper?
A copyable, assignable object holding a reference, created with std::ref/std::cref; used in containers and to pass references through std::thread, std::bind and algorithms.
16. Why return a const reference from a getter?
To give read access to a member without copying it, while preventing callers from modifying it.
17. Does sizeof(T&) give the size of a reference?
No, it gives sizeof(T) – the reference behaves like the object.
18. Can a function return an rvalue reference?
Yes (std::move itself does), but returning T&& to a local variable dangles. Functions normally return by value and rely on moves or copy elision.
19. What happens when you std::move a const object?
The result is const T&&, which cannot bind to a move constructor's T&&, so the copy constructor is used.
20. What is the lifetime of a temporary bound to a const reference?
It is extended to the lifetime of the reference – as long as the reference is bound directly to the temporary, not through a function return.