Function call order

for expression

(func1() * func2()) + func3() 

func1 () * func2 () will be evaluated first, since it has brackets or can be called in any order, for example,

first func3 () and then (func1 () * func2 ())

+6
c ++ c
source share
6 answers

Functions can be called in any order.

+13
source share

Operator precedence has nothing to do with the order in which operands are evaluated.

The C or C ++ standard does not define the order in which functions will be called.

The procedure for evaluating subexpressions, including

  • function call arguments and
  • operands of operators (for example, , +, -, =, * , / ), with the exception of:
    • binary logical operators ( && and || ),
    • ternary conditional operator ( ?: and
    • comma operator ( , )

Not specified

for example

  int Hello() { return printf("Hello"); /* printf() returns the number of characters successfully printed by it */ } int World() { return printf("World !"); } int main() { int a = Hello() + World(); //might print Hello World! or World! Hello /** ^ | Functions can be called in either order **/ return 0; } 
+8
source share

You cannot make any assumptions about the order in which these functions will be called. This is perfectly true for the compiler to call these functions in any order, assign the results as temporary, and then use these temporary values ​​to calculate the result of the expression.

+6
source share

These calls can be made in any order. Do you want to know about C ++ sequence points C ++ point sequence .

+4
source share

It is natural to think that A+B evaluates to C in this psudocode:

(A+b)*C

But actually it is not. The Standard states that the evaluation order for all “Unspecified” expressions, unless otherwise specified in the Standard:

5/4 [expr]:

Except where noted, the order of evaluation of the operands of individual operators and subexpressions of individual expressions and the order in which side effects occur is not defined

Then, the expression in parentheses is identified as “primary expression” in the standard, but the evaluation order for primary expressions is not specified. (5.1 / 5).

In the standard, "Unspecified" does not mean "Undefined". Rather, it means "Implementation is defined, but documentation is not required." That way, you won’t even be able to tell which evaluation order is for a particular compiler.

Here is a simple program illustrating the behavior:

 #include <iostream> #include <string> using namespace std; class Foo { public: Foo(const string& name) : name_(name) {++i_; cout << "'" << name << "'(" << i_ << ")\n"; }; operator unsigned() const { return i_; } Foo operator+(const Foo& rhs) const { string new_name = name_; new_name += "+"; new_name += rhs.name_; return Foo(new_name); } private: string name_; static unsigned i_; }; unsigned Foo::i_ = 0; int main() { (Foo("A") + Foo("B")) + Foo("C"); } 

On my MSVC10 running in Debug / x64 on Win7, the output was as follows:

 'C'(1) 'B'(2) 'A'(3) 'A+B'(4) 'A+B+C'(5) 
+2
source share

A bracket in C / C ++ forces the ordering of operations. func1() * func2() will be added to func3() , but the compiler can choose to call the functions in whatever order he wants before passing the results of the multiply / add operation.

+1
source share

All Articles