Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes, but global variables are not necessarily static.


I 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.


Fair enough.


All variables declared at file scope have static storage duration in C.


I was talking about using static keyword.


Yes, 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).


Use 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.


Just FYI, I believe that deprecated usage was later undeprecated. See https://stackoverflow.com/questions/4726570/deprecation-of-t...




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: