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 - |
문제
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?
해결책
다른 팁
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;
제휴하지 않습니다 StackOverflow