标准C 11是否保证在函数调用之前创建传递给函数的临时对象?

标准C 11是否保证在函数调用之前创建传递给函数的临时对象?,第1张

概述标准C 11是否保证在开始执行函数之前创建了所有3个临时对象? 即使临时对象传递为: >对象 > rvalue-reference >仅传递临时对象的成员 http://ideone.com/EV0hSP #include <iostream>using namespace std;struct T { T() { std::cout << "T created \n"; } 标准C 11是否保证在开始执行函数之前创建了所有3个临时对象?

即使临时对象传递为:

>对象
> rvalue-reference
>仅传递临时对象的成员

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;}

输出:

T created T created T created func-start 0,0func-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传递,以便在创建临时对象之前启动函数?

解决方法 [intro.execution]/16:

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是否保证在函数调用之前创建传递给函数的临时对象?全部内容,希望文章能够帮你解决标准C 11是否保证在函数调用之前创建传递给函数的临时对象?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1222068.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存