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

Source: Internet
Author: User

The role of anonymous namespace and its difference from static

One. The role of anonymous namespace
In the C language, if we use the same name in multiple Tu (translation unit)
is a function name or a global variable name, a redefinition error occurs during the link phase, in order to resolve the
Problem, we can add the Static keyword fix when defining these identifiers (identifier)
To limit its visibility to only one TU range.
C + + inherits the use of the static keyword in C, and we can still use static to avoid
A redefinition problem with the same identifier in multiple tu. In addition, C + + provides another unique
The way that is anonymous namespace: a namespace without a named name is called a stealth
Name namespace; Multiple anonymous namespace can appear in a tu, and the same level of anonymity
namespace is actually the same as the same one; appears in the anonymous namespace of different tu
There is no conflict between the characters, so we can see those that only want to be visible in the same Tu range.
's global identifier into an anonymous namespace, the effect is the same as the previous add static.

Two. 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 does not change the linkage of identifiers defined inside it, and it is used to avoid
Name collisions are used in the same way that C + + is used to implement overloading, that is, by using the name change
(name mangling): According to the C + + standard 7.3.1.1, anonymous namespace in each tu actually
will have a unique name, so the same identity in the anonymous namespace of different tu
Characters 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 anonymous namespace do not take the same practice as static, do a trick is not to increase
The burden of compiler development? This is actually because another C + + feature has pinned down anonymous namespace's
Implementation, which is the template non-type parameter (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 n Ame of a non-type template-parameter; or
-the address of an object or function with external linkage , Includi ng
Function templates and function Template-ids but excluding non-static
Class members, expressed as & Id-expres Sion where the & is optional
If the name refers to a function or array, or if the corresponding
Template-paramet ER 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
Want to be able to use its address to instantiate a template, how to do? Valid only in one Tu, you can
Select internal linkage, but use its address as the template parameter and require it to be
It's external linkage!!.
It is clear that the nature of anonymous namespace does not change its internal identifier linkage this
Problem, we can safely throw this global object or function into an anonymous namespace,
Then use its address to instantiate a template, never redefining the error:)

Most C + + books now think that anonymous namespace and static are the same, and as explained here
The difference between them is obvious: The identifier of the static modifier due to the internal linkage
Restrictions, is not used to instantiate the template!

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
struct FOO
{
void Bar ();
};

static char a = ' a ';

Namespace
{
Char b = ' B ';
static char C = ' C ';

Template struct XXX {};

void Foobar ()
{
struct No_linkage {};
Xxx<no_linkage> (); If compile error, the linkage of no_linkage is not changed
}
}

int main ()
{
Foo<&a> (). bar (); Because the linkage of a is internal, it should compile the error
Foo<&b> (). bar (); If the compilation is correct, the linkage of B is external
Foo<&c> (). bar (); If the compilation error, the 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 the difference from static

Related Article

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.