Should functions return const references?
Show answer and explanation
A function may return a const reference only when the referred object is guaranteed to outlive every use by the caller. It avoids a copy and prevents mutation through that reference, but returning a local or short-lived subobject dangles.
A function may return a const reference only when the referred object is guaranteed to outlive every use by the caller. It avoids a copy and prevents mutation through that reference, but returning a local or short-lived subobject dangles.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.