By Zeta

2018-10-11 14:33:27 8 Comments

If I do like this

f() + g();

or like this

h(f() + g());

Is it guaranteed in C++ that f() is called first?


@dbush 2018-10-11 14:35:20

C++ does not guarantee the order of evaluation of the operands of the + operator.

Either f() or g() may be called first.

This is specified by section 1.9p15 of the C++ standard:

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. — end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

The best way to handle this is to execute the functions in separate statements:

int a = f();
int b = g();
h(a + b);

@Zeta 2018-10-11 15:51:24

Thank you. Then how about this? (f()) + g(); Or is there any way to force f() to be called first?

@dbush 2018-10-11 15:54:38

@Zeta No difference. Parenthesis only dictate the order in which individual operators are resolved. It does not dictate the order of subexpressions. For example, in a + (b + c), any of a, b and c may be evaluated in any order, but b + c is evaluated before a + (b + c).

@dbush 2018-10-11 15:55:59

@Zeta If you want f() to be called first, you need to do so in a separate statement. For example auto f_r = f(); auto g_r = g(); h(f_r + g_r);

@Zeta 2018-10-11 16:49:02

I found one way of doing it in one line. h((auto a = f(), a + g()));

@dbush 2018-10-15 20:01:39

@Zeta That syntax is not valid, and even if it were it's cumbersome to have on one line like that. Better to break it out into multiple lines for clarity.

@Blaze 2018-10-11 14:35:11

There is no guarantee, according to this:

The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

Disclaimer: at least that's the case in C, I am not sure if it's the same in C++.

Edit: I just looked up the C++ version on the same site, and it's also unspecified in C++.

@Sourav Ghosh 2018-10-11 14:36:47

Are you linking to a C resource?

@Blaze 2018-10-11 14:37:28

@SouravGhosh I'm linking to the C subsection of a C++ resource.

@Sourav Ghosh 2018-10-11 14:38:10

and why you would do that?

@Blaze 2018-10-11 14:39:23

There was a C tag, too, until it got removed. But good call! I'm sure the C++ answer is on the same site. I just checked, and indeed, it is.

Related Questions

Sponsored Content

22 Answered Questions

34 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

86 Answered Questions

[SOLVED] How do JavaScript closures work?

21 Answered Questions

[SOLVED] What is the "-->" operator in C++?

26 Answered Questions

[SOLVED] How do you set, clear, and toggle a single bit?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2014457 View
  • 4251 Score
  • 1 Answer
  • Tags:   c++ c++-faq

21 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

33 Answered Questions

11 Answered Questions

[SOLVED] What does the explicit keyword mean?

7 Answered Questions

Sponsored Content