标准 C++11 是否保证在函数调用之前创建传递给函数的临时对象?
Does standard C++11 guarantee that temporary object passed to a function will have been created before function call?
标准 C++11 是否保证在开始执行函数之前创建了所有 3 个临时对象?
即使临时对象传递为:
- 对象
- 右值参考
- 只传递了临时对象的成员
#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;
}
输出:
T created
T created
T created
func-start
0, 0, 0
func-end
T destroyed
T destroyed
T destroyed
工作草案,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.
但是可以在func-start之后创建中的任何一个T吗?
或者有什么方法可以将参数作为 g/r/l/x/pr-value 传递,以便函数在创建临时对象之前启动?
从 [expr.call]/8 我们有
[ Note: The evaluations of the postfix expression and of the arguments are all unsequenced relative to one another. All side effects of argument evaluations are sequenced before the function is entered (see 1.9). —end note ]
这意味着在输入函数之前构造所有参数。
因此这也保证了函数退出后所有参数都被销毁
When calling a function (whether or not the function is inline), every
value computation and side effect associated with any argument
expression, or with the postfix expression designating the called
function, is sequenced before execution of every expression or
statement in the body of the called function.
标准 C++11 是否保证在开始执行函数之前创建了所有 3 个临时对象?
即使临时对象传递为:
- 对象
- 右值参考
- 只传递了临时对象的成员
#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;
}
输出:
T created
T created
T created
func-start
0, 0, 0
func-end
T destroyed
T destroyed
T destroyed
工作草案,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.
但是可以在func-start之后创建中的任何一个T吗?
或者有什么方法可以将参数作为 g/r/l/x/pr-value 传递,以便函数在创建临时对象之前启动?
从 [expr.call]/8 我们有
[ Note: The evaluations of the postfix expression and of the arguments are all unsequenced relative to one another. All side effects of argument evaluations are sequenced before the function is entered (see 1.9). —end note ]
这意味着在输入函数之前构造所有参数。
因此这也保证了函数退出后所有参数都被销毁
When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function.