"(F (x)) + g (y)" can make sure that calling f (x) in C ++ first?

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

+4
source share
3 answers

Brackets exist to override priority. They do not affect the evaluation order.

+24
source

Look ma, two lines!

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

This is an important sequence page between two function calls. There may be other ways to do this, but this way seems simple and obvious. Note that no sequence point is entered in your parentheses, so this is not a solution.

+12
source

No. If the + operator is not overridden, such things are evaluated left to right. Even if you could influence the priority in the statement, it would not necessarily mean that f and g were evaluated in the same order. If you need f evaluate to g , you can always:

 auto resultOfF = f(x); auto resultOfG = g(x); resultOfF + resultOfG; 
+2
source

Source: https://habr.com/ru/post/1489889/


All Articles