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
- Reduce avoidable object construction and destruction in C++ class operations and expression evaluation.
- How deep it goes
- Explain intermediate objects and constructor costs, then show operator fixes; note that compilers may optimize some temporaries.
- Examples
- Use a Complex class to contrast chained assignment by value with reference return and chained addition with +=.
- What you can do after
- Build C++ class operators that avoid unnecessary copies while preserving chained assignment and arithmetic behavior.
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 →
Also covered in
What to know first
- compound literalA compound literal is a C expression consisting of a parenthesized type name followed by an initializer that creates an unnamed object.
- objectAn object is a distinct entity represented or manipulated within a computational system, typically as a value, data structure, resource, or instance of a type.
- variable-length arrayA variable-length array is an array whose bound is determined at run time rather than fixed as a compile-time constant.
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.
- incrementused by · Incrementing is the operation of increasing a numeric value, typically 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.
- 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.
Questions about temporary object
What is temporary object?
A temporary object is an unnamed object created for an intermediate expression or initialization whose lifetime is limited by language rules.
Which Manning books explain temporary object?
100 C++ Mistakes and How to Avoid Them and Modern C, Third Edition.
What should I know before learning temporary object?
Manning books that teach temporary object usually introduce compound literal, object and variable-length array first.
What is temporary object a kind of?
Temporary object is a kind of object. A programming-language object is a runtime entity with a type or properties and associated state that participates in computation.
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.

