The role of C + + anonymous namespace and its difference from static

Source: Internet
Author: User

I. The role of anonymous namespace
In C, if we use the same name as function name or global variable name in multiple Tu (translation unit), a redefinition error occurs during the link phase, in order to solve this problem, We can add the static keyword modifier when defining these identifiers (identifier) to restrict it from being visible only within a tu range.
C + + inherits the use of the static keyword in the C language, and we can still use static to avoid the redefinition problem of using the same identifier in multiple tu. In addition, C + + provides another unique way, that is, anonymous namespace: A namespace without a specified name is called an anonymous namespace; Multiple anonymous namespace can appear in a Tu, And the same level of anonymous namespace is actually being the same; the same identifiers that appear in the anonymous namespace of different tu do not conflict with each other, so we can put those global identifiers that only want to be visible in the same TU scope into an anonymous namespace , the effect is the same as the previous plus static.

second, the difference between anonymous namespace and static
When a global identifier is modified by static, its linkage becomes internal linkage, which is why the same identifiers in different tu do not collide.
Anonymous namespace, however, does not change the linkage of identifiers defined inside it, and the means used to avoid name collisions are the same as those used by C + + to implement overloading, that is, by using name adaptation (name mangling): according to C + + Standard 7.3.1.1, the anonymous namespace in each tu will actually have a unique name, so the same identifiers in the anonymous namespace of different tu actually belong to different namespace, and naturally there will be no conflict after the name is adapted:

7.3.1.1 Unnamed namespaces [namespace.unnamed]
An unnamed-namespace-definition behaves as if it were replaced by
namespace Unique {/* empty body */}
using namespace unique;
Namespace Unique {Namespace-body}
Where all occurrences of the unique in a translation unit is replaced
By the same identifier and this identifier differs from all other
Identifiers
in the entire program.

Why is it that anonymous namespace does not take the same approach as static, does it add to the burden of compiler development? This is because another C + + feature has pinned down the implementation of anonymous namespace, which is the template non-type parameter (templates non-type arguments):

14.3.2 Template non-type arguments [Temp.arg.nontype]
A template-argument for a non-type, non-template template-parameter
shall be one of:
-an integral constant-expression of integral or enumeration type; Or
-the name of a non-type template-parameter; Or
-the address of an object or function with external linkage, including
function templates and function Template-ids but excluding non-static
Class members, expressed as & Id-expression where the-the & is optional
If the name refers to a function or array, or if the corresponding
Template-parameter is a reference; Or
-A pointer to member expressed as described in 5.3.1.


It is the external linkage that is marked by the Scarlet Letter. This requirement limits the implementation of anonymous Namespace! Imagine if we had a global object or function that only wanted it to work in a tu, and would like to be able to instantiate a template with its address. Valid only in one Tu, you can choose internal linkage, but use its address as a template parameter, and require it to be external linkage!!
Obviously, anonymous namespace does not change the nature of its internal identifier linkage This problem, we can put this global object or function at ease in an anonymous namespace, and then use its address to instantiate a template, there will never be a redefinition error:)

Most C + + books now think that anonymous namespace and static are the same, and as stated here, the difference between them is obvious: the static modifier identifier cannot be used to instantiate a template because of the internal linkage limit!

Finally give an example to confirm that anonymous namespace does not change linkage, hehe
External linkage/internal linkage/no Linkage Three cases verified in the code
-------------------------------------------------------------------------------

Template <char *p>struct foo{    void bar ();}; Static char a = ' A ';namespace{    char b =  ' B ';     static char c =  ' C ';    template <class t>  struct xxx {};    void foobar ()     {         struct no_linkage {};         xxx<no_linkage> ();  //  If compile error, No_linkage linkage is not changed     }}int  main () {    foo<&a> (). Bar ();  //  because the linkage of a is internal, So you should compile the error     foo<&b> (). Bar ();  //  if compiled correctly, linkage of B is external     foo<&c> (). Bar ();  //  If compile error, linkage of C is internal     foobar ();      return 0;} 

-------------------------------------------------------------------------------
Comeau/C + + 4.3.3 (6 2003 15:13:37) for Online_evaluation_beta1
Copyright 1988-2003 Comeau Computing. All rights reserved.
Mode:strict Errors C + +

"Comeautest.c", line 19:error:a template argument may not reference a
Local type
Xxx<no_linkage> ();
^
^

"Comeautest.c", line 25:error:a template argument may not reference a
Non-external entity
Hint:http://www.comeaucomputing.com/techtalk/templates/#stringliteral
Foo<&a> (). bar ();
^

"Comeautest.c", line 27:error:a template argument may not reference a
Non-external entity
Hint:http://www.comeaucomputing.com/techtalk/templates/#stringliteral
Foo<&c> (). bar ();
^

3 Errors detected in the compilation of "Comeautest.c".


The role of C + + anonymous namespace and its difference from static

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.