Search code examples
c++staticlinkage

Is static needed when declaring const globals?


often in headers I see

//global namespace, not in class
static const int my_global =1984;

but recently I learned that const implies internal linkage, so I wonder doesnt that make static unnecessary?


Solution

  • It depends. In C++, it's unnecessary, but some people (myself included) like to put it in, on the grounds of saying what we mean. And of course, if the header is to be used in C as well, it is necessary (but for many uses in C, you'll need a #define).