ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 返回对象的函数调用和该对象上的方法调用之间是否存在序列点?

2019-10-01 10:08:03  阅读:159  来源: 互联网

标签:c sequence-points unspecified-behavior


如果我写f(x) – > g(args,…)我可以依赖于f(x)之后的序列点来评估args,…?我可以通过两种方式看到论点:

>§1.9.17“当调用函数时(无论函数是否为内联函数),在评估函数体中任何表达式或语句之前发生的所有函数参数(如果有)之后都有一个序列点在复制返回值之后和执行函数之外的任何表达式之前,还有一个序列点.“
>另一方面,对象指针隐含地是一个隐藏的参数,就好像我写了g(f(x),args,…),这表明它就像一个参数,因此未指定.

– >运算符不是普通的二元运算符,因为在f(x)之前,如果我写f(x)g(…),就不能像g(…)那样进行评估.我很惊讶我找不到一些具体的说法.

解决方法:

答案取决于您使用的C标准版本(或您的编译器正在使用).

C 2003 5.2.2 p8说:

The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered. The order of evaluation of the postfix expression and the argument expression list is unspecified.

这意味着在评估f(x)和args之间没有序列点.

在C 2011中,序列点的整个概念已经被替换(见N1944),这个措辞现在只是一个注释:

[ Note: The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another. All side effects of argument expression evaluations are sequenced before the function is entered (see 1.9). — end note ]

和1.9 p15说

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. [ Note: Value computations and side effects associated with different argument expressions are unsequenced. — end note ]

这表示表达式f(x)和表达式args在g体中的所有内容之前排序,但是它们相对于彼此没有排序,这与C 03规则相同但措辞不同.

C 14与C 11具有相同的规则,但如下面的评论所述,规则在C 17中有所改变.

C 2017 8.2.2 [expr.call] p5说:

The postfix-expression is sequenced before each expression in the expression-list and any default argument. The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.

这意味着您的示例将按顺序执行以下步骤:

> f被评估.
>评估x并初始化f的参数.
>评估函数调用f(x).
评估> f(x) – > g.
> args和g的其他参数被计算,并且g的参数被初始化(以未指定的顺序).
>最后,评估函数调用f(x) – > g(args,…).

标签:c,sequence-points,unspecified-behavior
来源: https://codeday.me/bug/20191001/1838264.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有