Skip to content
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

Closed
AaronRobinsonMSFT opened this issue Dec 23, 2020 · 4 comments
Closed
Assignees

Comments

@AaronRobinsonMSFT
Copy link

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:

Use an unnamed (anonymous) namespace for all internal/non-exported entities

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:

// foo.cpp

namespace
{
   class HClass { ... };
   void HFunc() { ... }
}

// Class and function used here.

The LLVM guidelines are prescriptive between classes and functions:

make anonymous namespaces as small as possible, and only use them for class declarations

The Google style guide seems ambivalent and appears to feel both are valid:

place them in an unnamed namespace or declare them static

Official clarification on the matter would be welcome.

@cubbimew
Copy link
Member

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

@jwakely
Copy link
Contributor

jwakely commented Feb 25, 2021

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.

@MikeGitb
Copy link

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.

@hsutter
Copy link
Contributor

hsutter commented Apr 1, 2021

Editors call: Yes, we intend to discourage global static.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants