Question.6
A factory returns a large Packet object by value:
Packet
Packet create_packet(int id) { Packet p(id); p.fill_payload(); return p; // Return by value } Packet pkt = create_packet(42);
Is the return expensive or efficient?
Select Answer
Returns a dangling reference to a local variable
Expensive -- a full deep copy occurs on return
Depends on whether Packet has a move constructor
Efficient -- the compiler applies RVO (Return Value Optimization) to construct p directly in the caller's memory, or at worst, uses the move constructor (near zero cost)
p