C++: Life span of temporary arguments?

后端 未结 4 687
南旧
南旧 2020-11-22 09:58

When creating a new instance of a MyClass as an argument to a function like so:

class MyClass
{
  MyClass(int a);
};    

myFunction(MyClass(42))         


        
4条回答
  •  执念已碎
    2020-11-22 10:30

    In section 12.2, Temporary Objects, clause 3, the ANSI/ISO C standard states: "... Temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created."

    This is closely related to the concept of Sequence Points. When a sequence point is reached, all side-effects of expressions are guaranteed to have taken place.

提交回复
热议问题