Unnamed/anonymous namespaces vs. static functions

A feature of C++ is the ability to create unnamed (anonymous) namespaces, like so:

namespace {
    int cannotAccessOutsideThisFile() { ... }
} // namespace

You would think that such a feature would be useless -- since you can't specify the name of the namespace, it's impossible to access anything within it from outside. But these unnamed namespaces are accessible within the file they're created in, as if you had an implicit using-clause to them.

My question is, why or when would this be preferable to using static functions? Or are they essentially two ways of doing the exact same thing?


The C++ Standard reads in section 7.3.1.1 Unnamed namespaces, paragraph 2:

The use of the static keyword is deprecated when declaring objects in a namespace scope, the unnamed-namespace provides a superior alternative.

Static only applies to names of objects, functions, and anonymous unions, not to type declarations.

Edit:

The decision to deprecate this use of the static keyword (affect visibility of a variable declaration in a translation unit) has been reversed (ref). In this case using a static or an unnamed namespace are back to being essentially two ways of doing the exact same thing. For more discussion please see this SO question.

Unnamed namespaces still have the advantage of allowing you to define translation-unit-local types. Please see this SO question for more details.

Credit goes to Mike Percy for bringing this to my attention.


Putting methods in an anonymous namespace prevents you from accidentally violating the One Definition Rule, allowing you to never worry about naming your helper methods the same as some other method you may link in.

And, as pointed out by luke, anonymous namespaces are preferred by the standard over static members.


There is one edge case where static has a surprising affect (at least it was to me). The C++03 Standard states in 14.6.4.2/1:

For a function call that depends on a template parameter, if the function name is an unqualified-id but not a template-id, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:

  • For the part of the lookup using unqualified name lookup (3.4.1), only function declarations with external linkage from the template definition context are found.
  • For the part of the lookup using associated namespaces (3.4.2), only function declarations with external linkage found in either the template definition context or the template instantiation context are found.
  • ...

    The below code will call foo(void*) and not foo(S const &) as you might expect.

    template <typename T>
    int b1 (T const & t)
    {
      foo(t);
    }
    
    namespace NS
    {
      namespace
      {
        struct S
        {
        public:
          operator void * () const;
        };
    
        void foo (void*);
        static void foo (S const &);   // Not considered 14.6.4.2(b1)
      }
    
    }
    
    void b2()
    {
      NS::S s;
      b1 (s);
    }
    

    In itself this is probably not that big a deal, but it does highlight that for a fully compliant C++ compiler (ie. one with support for export ) the static keyword will still have functionality that is not available in any other way.

    // bar.h
    export template <typename T>
    int b1 (T const & t);
    
    // bar.cc
    #include "bar.h"
    template <typename T>
    int b1 (T const & t)
    {
      foo(t);
    }
    
    // foo.cc
    #include "bar.h"
    namespace NS
    {
      namespace
      {
        struct S
        {
        };
    
        void foo (S const & s);  // Will be found by different TU 'bar.cc'
      }
    }
    
    void b2()
    {
      NS::S s;
      b1 (s);
    }
    

    The only way to ensure that the function in our unnamed namespace will not be found in templates using ADL is to make it static .

    Update for Modern C++

    As of C++ '11, members of an unnamed namespace have internal linkage implicitly (3.5/4):

    An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage.

    But at the same time, 14.6.4.2/1 was updated to remove mention of linkage (this taken from C++ '14):

    For a function call where the postfix-expression is a dependent name, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:

  • For the part of the lookup using unqualified name lookup (3.4.1), only function declarations from the template definition context are found.

  • For the part of the lookup using associated namespaces (3.4.2), only function declarations found in either the template definition context or the template instantiation context are found.

  • The result is that this particular difference between static and unnamed namespace members no longer exists.

    链接地址: http://www.djcxy.com/p/95246.html

    上一篇: 高级Unicode GUI用于Windows / Linux / Mac的C ++本机应用程序

    下一篇: 未命名/匿名命名空间与静态函数