@@ -1594,20 +1594,25 @@ Restrict Visibility
1594
1594
^^^^^^^^^^^^^^^^^^^
1595
1595
1596
1596
Functions and variables should have the most restricted visibility possible.
1597
+
1597
1598
For class members, that means using appropriate ``private ``, ``protected ``, or
1598
- ``public `` keyword to restrict their access. For non-member functions, variables,
1599
- and classes, that means restricting visibility to a single ``.cpp `` file if it's
1600
- not referenced outside that file.
1599
+ ``public `` keyword to restrict their access.
1600
+
1601
+ For non-member functions, variables, and classes, that means restricting
1602
+ visibility to a single ``.cpp `` file if it is not referenced outside that file.
1601
1603
1602
1604
Visibility of file-scope non-member variables and functions can be restricted to
1603
1605
the current translation unit by using either the ``static `` keyword or an anonymous
1604
- namespace. Anonymous namespaces are a great language feature that tells the C++
1606
+ namespace.
1607
+
1608
+ Anonymous namespaces are a great language feature that tells the C++
1605
1609
compiler that the contents of the namespace are only visible within the current
1606
1610
translation unit, allowing more aggressive optimization and eliminating the
1607
- possibility of symbol name collisions. Anonymous namespaces are to C++ as
1608
- ``static `` is to C functions and global variables. While ``static `` is available
1609
- in C++, anonymous namespaces are more general: they can make entire classes
1610
- private to a file.
1611
+ possibility of symbol name collisions.
1612
+
1613
+ Anonymous namespaces are to C++ as ``static `` is to C functions and global
1614
+ variables. While ``static `` is available in C++, anonymous namespaces are more
1615
+ general: they can make entire classes private to a file.
1611
1616
1612
1617
The problem with anonymous namespaces is that they naturally want to encourage
1613
1618
indentation of their body, and they reduce locality of reference: if you see a
@@ -1653,10 +1658,17 @@ Avoid putting declarations other than classes into anonymous namespaces:
1653
1658
1654
1659
} // namespace
1655
1660
1656
- When you are looking at "``runHelper ``" in the middle of a large C++ file,
1657
- you have no immediate way to tell if this function is local to the file. In
1658
- contrast, when the function is marked static, you don't need to cross-reference
1659
- faraway places in the file to tell that the function is local.
1661
+ When you are looking at ``runHelper `` in the middle of a large C++ file,
1662
+ you have no immediate way to tell if this function is local to the file.
1663
+
1664
+ In contrast, when the function is marked static, you don't need to cross-reference
1665
+ faraway places in the file to tell that the function is local:
1666
+
1667
+ .. code-block :: c++
1668
+
1669
+ static void runHelper() {
1670
+ ...
1671
+ }
1660
1672
1661
1673
Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements
1662
1674
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 commit comments