static storage duration. The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists.
Your variable and it's value persists between function calls.
#include <iostream>
void func();
int main()
{
for (int i { }; i < 10; i++)
{
func();
}
}
void func()
{
// created every time the function is called
int var1 { }; // <--- initialized to zero via uniform initialization
// created ONCE, when the function is called the first time
// each subsequent call the value is retained from the previous call
staticint var2 { };
std::cout << var1 << ' ' << var2 << '\n';
// change the variables' values
var1++; var2++;
}
It is completely safe even in the presence of multiple threads.
The initialisation does not recursively reenter the block.
It is an object of a const-qualified type; so immutable after it is initialised.
Since they are const, do they need to be strings? You could save the program time and space by using the C strings directly: staticconstchar *find[] = {"val1", "val2"};
The overhead of using std::string would be quite irrelevant here; these are constructed just once in the program.
If space is actually a concern use std::string_view; that would not compromise code quality / introduce extra programmer overhead. static const std::array< std::string_view, 2 > find { { "val", "val2" } };