문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top