The precise wording of the standard is:
The use of the static
keyword is deprecated when declaring objects in namespace scope.
Functions in a header file should be inline
rather than static
or in an unnamed namespace. inline
means you will only end up with at most one copy of the function in your program, while the other methods will give you a separate copy from each file that includes the header. As well as bloat, this could give incorrect behaviour if the function contains function-static data. (EDIT: unless the function is supposed to have different definitions in different compilation units, perhaps due to different preprocessor macros that are defined before including the header file. In that case the best approach is not to include it at all, but rather to bury it in an unmarked grave with a stake through its unholy heart.)
Data objects, apart from constants, usually shouldn't be defined in header files at all, only declared extern
.
Static member functions are a different kettle of fish, and you have to use static
there as there is no other way to declare them. That usage isn't deprecated, since it isn't in namespace scope.
UPDATE: C++11 has removed the deprecation, so there's no longer any particular reason to prefer unnamed namespaces over static
. But you still shouldn't use either in a header file unless you're doing something weird.