Why is an unnamed namespace used instead of static?
In C++, some uses of the static keyword have been deprecated. In particular, an unnamed namespace should be favored over some previous uses of “file scope static’s”. In fact in some cases an unnamed namespace must be used in order to obtain a similar effect. That is to say, this code: // x.cpp static bool flag = false; // AAA void foo() { if (flag)… } void bar() { …flag = true… } should instead often be composed this way in C++: // x.cpp namespace /* NOTHING HERE!! */ { // BBB bool flag = false; // no need for static here } The use of static in AAA indicates that flag has internal linkage. This means that flag is local to its translation unit (that is, effectively it is only known by its name in some source file, in this case x.cpp). This means that flag can’t be used by another translation unit (by its name at least). The goal is to have less global/cross-file name pollution in your programs while at the same time achieving some level of encapsulation. Such a goal is usually con