Question

Give an example in C++ of an object whose lifetime has surpassed the lifetime of any...

Give an example in C++ of an object whose lifetime has surpassed the lifetime of any binding to that object. What specifically is this called?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Temporary objects are created when a prvalue is materialized so that it can be used as a glvalue, which occurs (since C++17) in the following situations:

  • binding a reference to a prvalue
  • returning a prvalue from a function
  • conversion that creates a prvalue (including T(a,b,c) and T{})
  • lambda expression, (since C++11)
  • copy-initialization that requires conversion of the initializer,
  • list-initialization that constructs an std::initializer_list, (since C++11)
  • reference-initialization to a different but convertible type or to a bitfield.
  • when initializing an object of type std::initializer_list<T> from a braced-init-list
  • when performing member access on a class prvalue
  • when performing an array-to-pointer conversion or subscripting on an array prvalue
  • for unevaluated operands in sizeof and typeid
  • when a prvalue appears as a discarded-value expression
  • if supported by the implementation, when passing or returning an object of trivially-copyable type in a function call expression (this models passing structs in CPU registers)

All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation. This is true even if that evaluation ends in throwing an exception.

There are two exceptions from that:

  • The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11)
Add a comment
Know the answer?
Add Answer to:
Give an example in C++ of an object whose lifetime has surpassed the lifetime of any...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT