Self-obfuscation: priority issues

Source: Internet
Author: User

Because I have learned how to compile, I always think that the computer is read from left to right... therefore, there is a potential consciousness that the priority order in C ++ is also viewed from left to right,Then you can view the priority of the symbol,In fact, in C ++, except for logic and logic or, C ++ does not clearly define the sequence of binary operators,

The compiler is free to provide the best implementation method. Only when the "implementation efficiency" andProgramSeek a balance between "potential defects" in language use.

Because the order in which the operands are solved usually has no effect on the result.Only when the two operands of the binary operator involve the same objectAnd change the value of this object, the order in which the operands are solved will affect the calculation result. The latter case will only appear in some (or even a few) programs. In practice, this "potential defect" can be compensated by the efforts of programmers, but the improvement in "implementation efficiency" can benefit all the programs that use the compiler, therefore, it must be greater than the disadvantage.

 

For example, if (IA [index ++] <Ia [Index]):
The behavior of this expression is not clearly defined. The problem is: <both the left and right operands use the index variable, but the left operand changes the value of this variable. Assume that the initial index value is 0.

The interpreter can evaluate the value of this expression in one of the following two ways. The C ++ language does not guarantee the computing order from left to right.

If (IA [0] <Ia [0]) // execution if RHS is evaluated first
If (IA [0] <Ia [1]) // execution if LHS is evaluated first
Depends on the Compiler

 

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.