From b0bff9eefe969926df88233a65d7724c5c23dcac Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 1 Jan 2006 21:59:22 +0000 Subject: Add a section about using namespaces. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25054 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/CodingStandards.html | 55 ++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-) (limited to 'docs/CodingStandards.html') diff --git a/docs/CodingStandards.html b/docs/CodingStandards.html index 35ead311fc..84b9adb66a 100644 --- a/docs/CodingStandards.html +++ b/docs/CodingStandards.html @@ -45,9 +45,9 @@
  • The Low Level Issues
    1. Assert Liberally
    2. +
    3. Do not use 'using namespace std'
    4. Prefer Preincrement
    5. Avoid std::endl
    6. -
    7. Exploit C++ to its Fullest
  • See Also
  • @@ -535,6 +535,40 @@ assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!" + +
    + Do not use 'using namespace std' +
    + +
    +

    In LLVM, we prefer to explicitly prefix all identifiers from the standard +namespace with an "std::" prefix, rather than rely on "using namespace std;". +

    + +

    In header files, adding a 'using namespace XXX' directive pollutes the +namespace of any source file that includes the header. This is clearly a bad +thing.

    + +

    In implementation files (e.g. .cpp files) the rule is more of a stylistic +rule, but is still important. Basically, using explicit namespace prefixes +makes +the code more clear - because it is immediately obvious what facilities +are being used and where they are coming from - and more portable - +because namespace clashes cannot occur between LLVM code and other namespaces. +The portability rule is important because different standard library +implementations expose different symbols (potentially ones they shouldn't) and +future revisions to the C++ standard will add more symbols to the std +namespace. As such, we never 'using namespace std;' in LLVM.

    + +

    The exception to the general rule (i.e. it's not an exception for the std +namespace) is for implementation files. For example, all of the code in the +LLVM project implements code that lives in the 'llvm' namespace. As such, it +is ok, and actually more clear, for the .cpp files to have a 'using namespace +llvm' directive at their top, after the #includes. The general form of this +rule is that any .cpp file that implements code in any namespace may use that +namespace (and its parents), but should not use any others.

    + +
    @@ -579,25 +613,6 @@ it's better to use a literal '\n'.

    - -
    - Exploit C++ to its Fullest -
    - -
    - -

    C++ is a powerful language. With a firm grasp on its capabilities, you can -make write effective, consise, readable and maintainable code all at the same -time. By staying consistent, you reduce the amount of special cases that need -to be remembered. Reducing the total number of lines of code you write is a -good way to avoid documentation, and avoid giving bugs a place to hide.

    - -

    For these reasons, come to know and love the contents of your local -<algorithm> header file. Know about <functional> -and what it can do for you. C++ is just a tool that wants you to master it.

    - -
    -
    See Also -- cgit v1.2.3