-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarification on when to use anonymous namespace vs static keyword #1727
Comments
FWIW the standard had at first deprecated static in favor of anon namespace, but went back purely because actually removing this deprecated static would never happen in practice: http://wg21.link/cwg1012 |
Maybe the llvm rule dates from C++03 when an unnamed namespace didn't imply internal linkage. That meant that functions in an unnamed namespace were still extern, just with an unpronounceable name. Since C++11 that's no longer true, declarations in unnamed namespace have internal linkage, so there's no technical reason to prefer static. |
The llvm guideline states local reasoning as the reason: I.e. in the middlle of a larger annonymous namespace, you don't see that you are in it anymore, so in order to validate that an entity has internal linkage, you need to scroll around in the file. For me, this is actually an argument in favor of unnamed namespaces: One less decoration necessary in addition to constexpr, noexcept, [[nodiscard]] ... and thus less visual noise distracting me from the core function signature. In nay case, the argument is still valid in c++11 and later. |
Editors call: Yes, we intend to discourage global |
On a code review a discussion when to use anonymous namespaces vs
static
was started. I wanted to confirm what appears to be the C++ Core Guidelines perspective and reconcile that with the current LLVM and Google style guides.The C++ Core Guidelines - SF.22 states:
which seems to imply that static keyword usage should be avoided when possible? Additional details for that rule do state "consider" so the rule is a bit vague given the additional "???" marks. This question could also be applied to I.2 for global variables, but that doesn't seem to mentioned functions or classes defined within a translation unit - for example:
The LLVM guidelines are prescriptive between classes and functions:
The Google style guide seems ambivalent and appears to feel both are valid:
Official clarification on the matter would be welcome.
The text was updated successfully, but these errors were encountered: