C++ reminder: Function-local static variables are initialized only once, even if it looks like they get initialized multiple times
Permanent link:
cppdashboard.dev/r/2026/09/cpp-reminder-function-local-static-variables-are-initializedThis article explains the behavior of function-local static variables in C++, emphasizing that they are initialized only once at the first point of declaration. Subsequent accesses retain the variable's value without reinitialization.
From the article
When you write a static variable inside a function, it is initialized only once, specifically at the first time that execution reaches the variable’s declaration , If execution reaches the variable again in the future, no initialization occurs. It just retains its old value.
Some time ago, I noted an attempt to fix a lifetime issue by making a variable static.
The original code used this header from an external widget library:
Share this resource