1

Suppose we have a following function, which is executed by multiple threads at the same time:

void foo()
{
    static SomeClass s{get_first_arg(), get_second_arg()};
}

My question is how many times will get_first_arg and get_second_arg be executed -- exactly once, or ThreadCount times, or is it unspecified/implementation-defined behavior?

i cant
  • 401
  • 2
  • 9
  • 2
    static local variables are created only once. [C++ static local variables not changing value](https://stackoverflow.com/questions/19544928/c-static-local-variables-not-changing-value) – Jason Oct 24 '22 at 10:21
  • @JasonLiam yeah, I know that. But how many times are its arguments created? – i cant Oct 24 '22 at 10:22
  • If the variable `s` is created only once, then why do you think that the arguments will be created/evaluated more than once? – Jason Oct 24 '22 at 10:23
  • 2
    _"...On all further calls, the declaration is skipped...."_ https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables – Richard Critten Oct 24 '22 at 10:28
  • @JasonLiam to me it also sounds absurd, but at the same time I couldn't find anywhere which behavior is expected. – i cant Oct 24 '22 at 10:28
  • @icant down to the assembly level, arguments are just local variables. – thedemons Oct 24 '22 at 10:30
  • Sometimes I wonder, do stackoverflow moderators even read questions. None of "duplicated" questions had an answer, and I asked about a completely different thing. – i cant Oct 24 '22 at 10:33
  • @RichardCritten yeah, that seems to be an answer. Thank you very much! – i cant Oct 24 '22 at 10:34
  • @SamVarshavchik There was no need to reopen the question. The [dupe](https://stackoverflow.com/a/36347433/12002570) already answers the question: *"On all further calls, the declaration is skipped."* – Jason Oct 24 '22 at 11:24
  • @SamVarshavchik [This is an exact dupe](https://stackoverflow.com/questions/36347380/function-executing-once) for this question. – Jason Oct 24 '22 at 11:26
  • @icant Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. Also, this is explained in any [beginner c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Oct 24 '22 at 11:27

1 Answers1

2

The local static variable s initialized only once and the first time control passes through the declaration of that variable. This can be seen from static local variable's documentation:

Variables declared at block scope with the specifier static have static storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

(emphasis mine)

This in turn means that the arguments get_first_arg() and get_second_arg() will be evaluated only once.


Jason
  • 36,170
  • 5
  • 26
  • 60
  • 1
    The exact same thing is explained in [this duplicate](https://stackoverflow.com/questions/36347380/function-executing-once). I closed this as a dupe but then the question was incorrectly reopened. – Jason Oct 24 '22 at 11:31