Does standard C++11 guarantee that all 3 temporary objects have been created before the beginning performe the function?
Even if temporary object passed as:
- object
- rvalue-reference
- passed only member of temporary object
#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 Programming Language C++ 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 containing a possibly empty, comma-separated list of initializer-clauses which constitute the arguments to the function.
But can be any of T created after func-start?
Or is there any way to pass arguments as g/r/l/x/pr-value so that the function started before the temporary object be created?
From [expr.call]/8 we have
This means that all parameters are constructed before the function is entered.
Consequently this also guarantees that all parameters are destroyed after the function exits.
[intro.execution]/16: