Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "(f(x))+g(y)" can make sure call f(x) first in C++?

And does f(x)+(g(y)) can make sure call g(y) first? I know the order in expression is undefined in many case, but in this case does parentheses work?

like image 419
ShenDaowu Avatar asked Nov 27 '22 00:11

ShenDaowu


2 Answers

Parentheses exist to override precedence. They have no effect on the order of evaluation.

like image 55
R. Martinho Fernandes Avatar answered Nov 29 '22 13:11

R. Martinho Fernandes


Look ma, two lines!

auto r = g(y);
f(x) + r;

This introduces the all-important sequence point between the two function calls. There may be other ways to do it, but this way seems straightforward and obvious. Note that your parentheses do not introduce a sequence point, so aren't a solution.

like image 37
John Zwinck Avatar answered Nov 29 '22 15:11

John Zwinck