Reference to an unnamed temporary object (life time)

前端 未结 3 1389
醉话见心
醉话见心 2021-01-14 00:04

After reading this answer from ildjarn, I wrote the following example, and it looks like an unnamed temporary object has the same life time as its reference!

  • H
3条回答
  •  悲&欢浪女
    2021-01-14 00:43

    A temporary bound to a const reference increases the lifetime of the temporary till the lifetime of the constant reference.

    Good Read:

    GotW #88: A Candidate For the “Most Important const”


    Yes it is specified in the C++ standard from the time references were introduced.
    So if you are wondering if this is C++11 feature, no it is not. It already existed in C++03.

提交回复
热议问题