What are expression templates, and what performance and lifetime trade-offs come with deferred evaluation?
Show answer and explanation
Expression templates overload operators to build a compile-time tree of lightweight node objects instead of eagerly materializing every intermediate result. A destination can traverse that tree once, enabling operation fusion and specialization, but the technique increases template complexity, compile time, code size, and diagnostic cost. Nodes commonly hold references to operands, so storing an expression beyond its operands can dangle; repeated evaluation, aliasing, and unexpected evaluation order also need explicit contracts. Modern compilers may already remove simple temporaries, so measured benefit should justify the abstraction.
Expression templates overload operators to build a compile-time tree of lightweight node objects instead of eagerly materializing every intermediate result. A destination can traverse that tree once, enabling operation fusion and specialization, but the technique increases template complexity, compile time, code size, and diagnostic cost. Nodes commonly hold references to operands, so storing an expression beyond its operands can dangle; repeated evaluation, aliasing, and unexpected evaluation order also need explicit contracts. Modern compilers may already remove simple temporaries, so measured benefit should justify the abstraction.
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.