I think it is guaranteed that f
is only called once.
First we have
The condition shall be of integral type, enumeration type, or class type.
[6.4.2 (1)] (the non-integral stuff does not apply here), and
The value of a condition that is an expression is the value of the
expression
[6.4 (4)]. Furthermore,
The value of the condition will be referred to as simply “the condition” where the
usage is unambiguous.
[6.4 (4)] That means in our case, the "condition" is just a plain value of type int
, not f
. f
is only used to find the value for the condition. Now when control reaches the switch
statement
its condition is evaluated
[6.4.2 (5)], i.e. we use the value of the int
that is returned by f
as our "condition". Then finally the condition (which is a value of type int
, not f
), is
compared with each case constant
[6.4.2 (5)]. This will not trigger side effects from f
again.
All quotes from N3797. (Also checked N4140, no difference)