Temporary object
A temporary object is an unnamed object created for an intermediate expression or initialization whose lifetime is limited by language rules.
100 C++ Mistakes and How to Avoid Them
A temporary object is an unnamed object created for an intermediate expression or value and typically given a limited lifetime.
- How it is used
- Uses temporary objects to identify avoidable construction and destruction costs in class operations and arithmetic expressions.
- How deep it goes
- Explains intermediate results, constructor and destructor costs, and operator remedies, but does not develop detailed lifetime rules.
- Examples
- Demonstrates Complex assignment, chained addition, operator+=, and incremental accumulation.
- What you can do after
- Diagnose unnecessary temporaries and reduce them by returning references and applying compound assignment where appropriate.
Read how 100 C++ Mistakes and How to Avoid Them explains temporary object →
Modern C, Third Edition
A temporary object is an unnamed C object created for an intermediate expression or initialization and given automatic temporary lifetime.
- How it is used
- Uses unnamed intermediate objects to explain automatic storage and compile-time type checking.
- How deep it goes
- Explains implicit creation and optimization, but stops short of a complete lifetime model for every temporary-expression context.
- Examples
- Demonstrates a returned struct accessed through mem().ory[0] and a compound literal used for macro argument checking.
- What you can do after
- Evaluate C expressions that create unnamed objects and recognize compound-literal techniques for assignment-compatibility checks.
Read how Modern C, Third Edition explains temporary object →
Related concepts
- copy assignment operatorused by · A copy assignment operator is a C++ special member function that copies state from one object into an already existing object of a compatible type.
- return value optimizationaddressed by · Return value optimization is a compiler optimization that constructs a function's returned object directly in the destination storage, eliminating unnecessary temporary objects and copy operations.
- incrementused by · Incrementing is the operation of increasing a numeric value by one.
- read-only objectkind of · A read-only object is an object that a program is not permitted to modify through its applicable access or type qualification.
- throw by valueused by · Throw by value is the C++ exception-handling technique of throwing an exception object as a value rather than as a pointer or reference.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

