Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top