37 lines · plain
1.. title:: clang-tidy - misc-use-anonymous-namespace2 3misc-use-anonymous-namespace4============================5 6Finds instances of ``static`` functions or variables declared at global scope7that could instead be moved into an anonymous namespace.8 9Anonymous namespaces are the "superior alternative" according to the C++10Standard. ``static`` was proposed for deprecation, but later un-deprecated to11keep C compatibility [1]. ``static`` is an overloaded term with different12meanings in different contexts, so it can create confusion.13 14The following uses of ``static`` will *not* be diagnosed:15 16* Functions or variables in header files, since anonymous namespaces in headers17 is considered an antipattern. Allowed header file extensions can be18 configured via the global option `HeaderFileExtensions`.19* ``const`` or ``constexpr`` variables, since they already have implicit20 internal linkage in C++.21 22Examples:23 24.. code-block:: c++25 26 // Bad27 static void foo();28 static int x;29 30 // Good31 namespace {32 void foo();33 int x;34 } // namespace35 36[1] `Undeprecating static <https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1012>`_37