Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions llvm/docs/CodingStandards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1579,17 +1579,23 @@ clarification.

.. _static:

Anonymous Namespaces
^^^^^^^^^^^^^^^^^^^^

After talking about namespaces in general, you may be wondering about anonymous
namespaces in particular. Anonymous namespaces are a great language feature
that tells the C++ compiler that the contents of the namespace are only visible
within the current translation unit, allowing more aggressive optimization and
eliminating the possibility of symbol name collisions. Anonymous namespaces are
to C++ as "static" is to C functions and global variables. While "``static``"
is available in C++, anonymous namespaces are more general: they can make entire
classes private to a file.
Restrict Visibility
^^^^^^^^^^^^^^^^^^^

Functions and variables should have the most restricted visibility possible.
For class members, that means using appropriate `private`, `protected` or `public`
keyword to restrict their access. For non-member functions, variables and classes,
that means restricting visibility to a single `.cpp` file if it's not referenced
outside that file.

Visibility of file-scoped non-members variables and functions can be restricted to
the current translation unit by using either `static` keyword or anonymous namespace.
Anonymous namespaces are a great language feature that tells the C++ compiler that
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the great here feels a bit like editorializing to me, while I don't disagree it kind of stands out wrt to the rest of the paragraph. I wouldn't bother changing it but for future reference.

the contents of the namespace are only visible within the current translation unit,
allowing more aggressive optimization and eliminating the possibility of symbol
name collisions. Anonymous namespaces are to C++ as `static` is to C functions and
global variables. While `static` is available in C++, anonymous namespaces are more
general: they can make entire classes private to a file.

The problem with anonymous namespaces is that they naturally want to encourage
indentation of their body, and they reduce locality of reference: if you see a
Expand Down