C + + evaluation order

Source: Internet
Author: User
Tags scalar

《C++Primer5th》中文版第124页C++语言没有明确规定大多数二元运算符的求值顺序,给编译器优化留下了余地。这种策略实际上是在代码生成效率和程序潜在缺陷之间进行了权衡,这个是否可以接受?
1. It is first known that the precedence specifies the combination of operands, but does not indicate in what order the operands are evaluated.

Like what:

int i=f1()*f2();

Although F1 and F2 are called before multiplication, it is not known whether F1 call first or F2 first.
2. Another example of binding law:
--

int i=0;cout<<i<<" "<<+i<<endl;

The result may be 0 1 or 1 1.
Because although << is a left-associative, the Order of evaluation is independent of precedence and associativity for operators that do not explicitly define the order of evaluation of operands.
So the above equation is undefined, that is, if the expression points to and modifies the same object, the behavior is undefined

    • Logic and &&
    • Logical Non | |
    • Conditions? :
      Comma
      The above four operators explicitly specify the order of evaluation of the operands.
3.c++ Handbook

The order of evaluation for almost all C + + operators, including the order of the function arguments in the function call expression and the Order of evaluation of the subexpression in any expression, is unspecified. The compiler can evaluate in any order , and you can select another order when the same expression is evaluated again.
Example:
Expression F1 () + F2 () + F3 ()
Because Operator+ 's left-to-right binding analysis is (F1 () + F2 ()) + F3 (), the runtime's function calls to F3 may be evaluated first, last, or between F1 () and F2 ().
4. Sequence point rule (the following is from the C + + manual)
--
Sequence point rule (before c++11)
Defined
Evaluation can have a side effect: the object that accesses a volatile lvalue, modifies an object, invokes a library I/O function, or calls a function that does any of these actions.

Sequence points (sequence point) are points in the execution sequence, where the side effects of all previous evaluation from the sequence have been completed, and the secondary effect of the subsequent evaluation has not started.

Rules
1) Each complete expression end (typically in a semicolon) has a sequence point.

2) When the function is called (whether or not the function is inline, regardless of whether the function call syntax is used), the evaluation of all function arguments (if any) has a sequence point that occurs before any expression or statement in the body of the function executes.

3) After the copy function returns a value, there is a sequence point before execution of any statement outside the function.

4) Once the function is executed, the expression from the caller function (the function cannot be interleaved) is not evaluated until the called function finishes.

5) For each of the following four expressions using the built-in (non-overloaded) operator, there is a sequence point after the evaluation of the expression A.

a && ba || ba ? b : ca , b

No behavior defined

    • 1) The stored value of a scalar object can be modified at most once before and after the sequence point, otherwise the behavior is undefined.

      i = ++i + i++; // 未定义行为i = i++ + 1; // 未定义行为( C++17 前)i = ++i + 1; // 未定义行为( C++11 前)++ ++i; // 未定义行为( C++11 前)f(++i, ++i); // 未定义行为( C++17 前)f(i = -1, i = -1); // 未定义行为( C++17 前)
    • 2) The previous value of the scalar object modified by the access expression evaluation between the sequence points, must only be determined to determine the value to be stored. If accessed in any other way, the behavior is undefined.

cout << i << i++; // 未定义行为( C++17 前)a[i] = i++; // 未定义行为( C++17 前)

C + + evaluation order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.