Evaluation order of function parameters in C + +

Source: Internet
Author: User

In C + +, if a function's argument list contains more than one argument, the order in which the parameters are evaluated is indeterminate.

In Rectification's "C + + Program Design" (second edition), P94, GCC evaluates parameters on aright-to-left order . But I do not measure this.

#include <iostream>#include<iomanip>using namespacestd;voidTestintXinty) {cout<<'x'<< x <<"y"<< y <<Endl;}intMain () {inti =3; Test (i, I++); I=3; Test (I,++i); I=3; Test (i++, i); I=3; Test (++I, i); return 0;}

The output of this program is:

X4 y3x4 y4x3 y4x4 Y4

Thus, even for the same compiler, the parameter evaluation order is indeterminate.

In practice, try to avoid situations where multiple expressions are included in a statement , or if there is no interaction between multiple expressions .

Evaluation order of function parameters in C + +

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.