Consider:
namespace JohnsLib {
static bool foobar();
bool bar();
}
What implications does static
have here?
Consider:
namespace JohnsLib {
static bool foobar();
bool bar();
}
What implications does static
have here?
It changes the linkage from "external" to "static", making it invisible to the linker, and unusable from other compilation units. (Well, if other compilation units also include the header, they get their own separate copy)
static
at namespace scope means that it is local to a translation unit (i.e. source file). If you define the function in the header file and include this header into multiple C++ files, you won't get redefinition errors because all the functions will be unique(more correctly, the functions will have internal linkage). The same effect can be achieved by means of anonymous namespaces, for example
namespace JohnsLib
{
namespace
{
bool foobar() {definition here, won't cause redefinition errors}
}
bool bar();
}
The result of static
keyword in namespace scope (global or user defined namespace) is that such define object will not have external linkage; that is, it will not be available from other translation units and cannot be used as a (non-type one i.e. reference or pointer) template parameter.
In the C++ programming Language Bjarne states In C and C++ programs,
the keyword static is (confusingly) used to mean "use internal linkage". Don't use static except inside functions and classes.
In Sutter/Alexandrescu C++ Coding Standards Item 61 is "Don't define entities with linkage in a header file."