At which exact statement does this program exhibit Undefined behavior as per the C++ standard?

后端 未结 3 744
夕颜
夕颜 2020-12-21 09:05

(I am aware of the fact that returning address/reference to a variable local to the function should be avoided and a program should never do this.)


3条回答
  •  没有蜡笔的小新
    2020-12-21 10:00

    I would say #3. Alone, #2 doesn't actually do anything even though the referenced object is already out of scope. This isn't really a standards-related issue because it is the result of two mistakes made in succession:

    1. Returning a reference to an out-of-scope object followed by
    2. Use of a reference.

    Either in isolation has defined behavior. Whether the standard has anything to say regarding use of references to objects beyond the end of their lifetime is another matter.

提交回复
热议问题