Static and anonymous namespace

Possible duplicate:
No Name / Anonymous Namespaces and Static Functions

Is this completely redundant or could there be a reason for this?

namespace { static void f() { ... } } 
+8
c ++
source share
1 answer

It looks superfluous to me - either declared static, or is in an anonymous namespace, because it has an internal connection.

ยง3.5 / 3:

A name that has a namespace scope (3.3.6) has an internal relationship if this name:
- a template for a variable, function, or function that is explicitly declared static;

ยง 3.5 / 4:

[...] An unnamed namespace or a namespace declared directly or indirectly in an unnamed namespace has an internal relationship. [...] A name with a namespace scope that has not been provided with an internal link above has the same relationship as the encompassing namespace, if that name is a Variable; or
- function; or

So, as of now, it has an internal connection, because it is explicitly declared static. If it was not explicitly declared static, it would have an internal relationship because it was declared inside an unnamed namespace. The same effect anyway.

Please note that here I answer specifically about the function - there are several unclear cases when there is a difference when you are dealing with a type name (for example, class / struct / union), but I do not know about such a thing that applies in case functions.

As for what internal communication means, one of these places is actually quite straightforward and straightforward. It is probably best to quote definitions of all three possibilities (ยง3.5 / 2):

  • When a name has an external reference, the entity that it denotes can be called names from areas of other translation units or from other areas of the same translation unit.
  • When a name is internally referenced, the object that it denotes can refer to names from other areas in the same translation unit.
  • If the name does not have a binding, the object that it denotes cannot refer to names from other realms.

Note that italics are higher than those specified in the standard, and this is a way of saying that these sentences define what these phrases mean in the rest of the standard.

+3
source share

All Articles