3 ms·
The C11 standard is clear here on syntax for a static initializer. Read section 6.7.9, constraint 4: All the expressions in an initializer for an object tha
by bb88 9y ago
The C11 standard is clear here on syntax for a static initializer.
Read section 6.7.9, constraint 4:
All the expressions in an initializer for an object that
has static or thread storage duration shall be constant
expressions or string literals.
It's syntax, not initialization.
And that makes sense. However the memory is initialized before runtime could be via memcpy() it could be loaded as part of the executable and then mapped dynamically at runtime. That's what 5.1.2 is saying.
What 6.7.9 constraint 4 is saying, is that static variables can only be constant expressions.
- pjmlp 9y agoYes, but global variables are not necessarily static.
- bb88 9y agoI think you're missing the point entirely here. C++ has to run code before main if a ctor is in a class that's static. There's no other way to initialize that static object. C prevents this by requiring static storage to be initialized with constants.
- pjmlp 9y agoFair enough.
- caf 9y agoAll variables declared at file scope have static storage duration in C.
- pjmlp 9y agoI was talking about using static keyword.
- caf 9y agoYes, but what's important here is the storage duration, which the static keyword doesn't affect at file scope (it just affects the symbol scope).
- jmaygarden 9y agoUse of static in the context of that C snippet is deprecated in C++. One is supposed to use an unnamed namespace instead. namespace { int myfunc() { return 5; } int x; int y = 4; int z = myfunc(); }; int main() {} In my opinion, his point is still valid.
- j4_james 9y agoJust FYI, I believe that deprecated usage was later undeprecated. See https://stackoverflow.com/questions/4726570/deprecation-of-the-static-keyword-no-more https://stackoverflow.com/questions/4726570/deprecation-of-t...