Parameter order evaluation

前端 未结 2 741
温柔的废话
温柔的废话 2021-01-16 00:34

In previous versions of the standard (C++03) the order of evaluation of parameters to a function call was unspecified.

Has this been changed in subsequent version of

2条回答
  •  感情败类
    2021-01-16 00:53

    No this has not changed but there is a very recent proposal to change this: N4228: Refining Expression Evaluation Order for Idiomatic C++, this was part of the Pre-Urbana mailing that came out this October The introduction says (emphasis mine going forward):

    Expression evaluation order is a recurring discussion topic in the C++ community. In a nutshell, given an expression such as f(a, b, c), the order in which the sub-expressions f , a , b , c are evaluated is left unspecified by the standard. If any two of these sub-expressions happen to modify the same object without intervening sequence points, the behavior of the program is undefined. For instance, the expression f(i++, i) where i is an integer variable leads to undefined behavior

    it proposes:

    We propose to revise C++ evaluation rules to support decades-old idiomatic constructs and programming practices. A simple solution would be to require that every expression has a well-defined evaluation order. That suggestion has traditionally met resistance for various reasons. Rather, this proposes suggests a more targeted fix

    • Postfix expressions are evaluated from left to right. This includes functions calls and member section expressions.
    • Assignment expressions are evaluated from right to left. This includes compound assignments.
    • Operands to shift operators are evaluated from left to right

    Update

    Herb Sutter recently put out a poll on order of evaluation looking for some feedback from the community on what result we would expect from the following code:

    std::vector v = { 0, 0 };
    int i = 0;
    v[i++] = i++;
    std::cout << v[0] << v[1] << endl;
    

    This would seem to indicate the committee is looking at the topic of order of evaluation seriously but as we can see from the discussion this is controversial.

提交回复
热议问题