3 ms·
> These variables are not on the heap. They are statically allocated. Their visibility is the only thing that differentiates them from global variables and stat
by Someone 2y ago
> These variables are not on the heap. They are statically allocated. Their visibility is the only thing that differentiates them from global variables and static variables defined outside of functions.
In C++, there is a difference: function-static variables get initialized when control first passes into the function. That difference matters in cases where the initial value (partly) comes from a function call or from another global. For example, in C++ one can do:
int bar = -1;
void foo() {
static char * baz = malloc(bar);
…
}
void quux() {
bar = 13;
}
That’s fine as long as the code calls quux before ever calling foo.
They added that feature not to support such monstrosities, but because they wanted to support having static variables of class type.
If they were to design that today, I think they would require the initialization expression to be constexpr.
- j16sdiz 2y ago> If they were to design that today, I think they would require the initialization expression to be constexpr. Why would they? This (non-constexpr) semantic would be useful as lazy initialization. .. and C++ love tiny little features like these.