The encoder believes that nextFoo is only installed on the first run, and the last line is it right?
Yes. static local variables are initialized only once (and not every time a function is introduced). In C ++ 11, this is also guaranteed in thread safe mode. For paragraph 6.7 / 4 of the C ++ 11 standard:
[...] If the control enters the declaration at the same time when the variable is initialized, simultaneous execution should wait for the initialization to complete [...]
Note that if the initialization of the static object throws an exception, its initialization will be retried the next time function() entered (in this case, it does not matter, since the initialization of int cannot throw). From the same paragraph above:
[...] If initialization is completed by throwing an exception, the initialization is not complete, so it will be checked again the next time the control enters the declaration. [...]
source share