Does standard C ++ 11 provide a guarantee that a temporary object passed to a function will be created before the function is called?

Does standard C ++ 11 guarantee that all 3 temporary objects were created before the function started?

Even if the temporary object is passed as:

  • an object
  • Rvalue link
  • only a member of the temporary object is transferred

http://ideone.com/EV0hSP

#include <iostream> using namespace std; struct T { T() { std::cout << "T created \n"; } int val = 0; ~T() { std::cout << "T destroyed \n"; } }; void function(T t_obj, T &&t, int &&val) { std::cout << "func-start \n"; std::cout << t_obj.val << ", " << t.val << ", " << val << std::endl; std::cout << "func-end \n"; } int main() { function(T(), T(), T().val); return 0; } 

Output:

 T created T created T created func-start 0, 0, 0 func-end T destroyed T destroyed T destroyed 

Working draft, standard for the C ++ programming language 2016-07-12: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf

Β§ 5.2.2 function call

Β§ 5.2.2

1 A function call is a postfix expression, followed by parentheses, possibly containing a list of sections, separated by commas, that make up the arguments to the function.

But can any of T be created after func-start ?

Or is there a way to pass arguments as g / r / l / x / pr-value so that the function runs before the creation of a temporary object?

enter image description here

+3
source share
2 answers

[intro.execution] / 16 :

When a function is called (regardless of whether the function is built-in), each value calculation and side effect associated with any argument of the expression or postfix expression that denotes the called function is sequenced before each expression is executed or in the body of the called function.

+11
source

From [expr.call] / 8 we have

[Note. The evaluations of the postfix expression and arguments are independent of the other. All side effects of the argument estimates are sequenced before the function is entered (see 1.9). -end note]

This means that all parameters are created before the function is entered.

Therefore, it also ensures that all parameters will be destroyed after the function exits.

+7
source

All Articles