There is an important difference.
A static local variable is initialized on the first call, so that in subsequent calls its declaration is no longer used. In particular, the constructor of the static local variable is called only once.
A static const
is a static variable which is also a constant: thus, its value, once initialized on the first call, cannot be changed anymore.
A constexpr
expression is an expression that can be evaluated at compile time. Because of that, a constexpr
variable can, for instance, be used as nontype template parameter.
Generically, you would not be able to use a simple static const
as template parameter, because even if it is a const, its actual value might not be determined at compile time; for example:
void f(int x)
{
static const i = x;
std::array<int, i> a; // Compile error!
}
Though, variables of integral type, that are initialized as constant expression can still be used as nontype template parameters. So, in the code of the question, the variable i
of func2
could be used as nontype template parameter.