Parentheses exist to override precedence. They have no effect on the order of evaluation.
Does "(f(x))+g(y)" can make sure call f(x) first in C++?
-
02-06-2022 - |
Frage
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?
Lösung
Andere Tipps
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.
No. Unless the +
operator is redefined, things like that are evaluated left to right. Even if you were able to influence the precedence in the operator, it wouldn't necessarily mean that f
and g
were evaluated in the same order. If you need f
to be evaluated before g
, you can always do:
auto resultOfF = f(x);
auto resultOfG = g(x);
resultOfF + resultOfG;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow