C++: Life span of temporary arguments?

后端 未结 4 699
南旧
南旧 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:44

    The standard does indeed offer guarantees - from section 12.2/5:

    A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call

    However, in your code it is not clear if the parameter is being passed by reference or by value, though at some point a copy constructor which does take a reference will be used.

提交回复
热议问题