Consider several different static variables:
void foo(int x) { static int bar = x; static std::string s1 = "baz"; static std::string s2("baz"); static int i{2};
Do you also think that s1 should "assign" a value of "baz" every time a function is called? What about s2 ? What about i ?
None of these statements perform any assignment, they are all initialized, and they are executed only once. Just because the operator includes the = symbol does not make it an assignment.
The reason the language is defined this way is because it usually uses a local static variable to run the function once:
bool doInit() {
If init again assigned a value each time the function is called, then doInit() will be called several times and will not be able to use a one-time installation.
If you want to change the value every time he called, it's just ... just change it. But if you do not want it to continue to change, then there would be no way to do this if the language worked as you ask.
It would also be impossible to have a local variable static const :
void func() { static const bool init = doInit();
Unfortunately, this will try to change the init value with every call.
source share