Unnecessary use of unnamed C ++ namespaces

I always see the code in our company:

namespace { const MAX_LIMIT = 50; const std::string TOKEN = "Token"; } 

I am confused about why you need an anonymous namespace. On the one hand, you need a local translation unit for MAX_LIMIT AND TOKEN . But this has already been achieved without an anonymous namespace due to const . static const and simple const both reach the local translation unit.

On the other hand, you have no name conflicts if somewhere in your file there is a variable named the same.

 int foo() { std::string TOKEN = "MyToken"; // Clash! ::TOKEN vs TOKEN can be used. } 

This justifies the anonymous namespace. But how often do you need a variable name in your function that is actually accepted by a const variable declared outside of your function? My answer is never. Therefore, in practice, an unnamed namespace is not required for me. Any clues?

+6
source share
3 answers

In this particular case, the namespace is really redundant, because by default the variables of the const namespace scope have an internal default relationship.

Consider changing the code later. Perhaps one of the variables should not be const. But at the same time, non-const also changes the default connection. An anonymous namespace will maintain internal communication even after such a change. In other words, the anon namespace shares the problems associated with constant and communication. Whether this is good depends on the context.

Note that the same can be achieved using the static . Since this has the same effect as the anon namespace, the choice is mostly aesthetic and therefore opinion based.

The argument for using anon namespaces instead of static may be consistent. You cannot define types internally linked to static . Since some internal characters (types) cannot be defined outside of the anonymous namespace, you may have an agreement to define all the internal characters in the anonymous namespace. Of course, such a convention would - as a rule, conventions - be a matter of taste.

Your second counter argument seems to argue against a difference that doesn't exist. An anonymous namespace has no meaning to be called hidden within the scope.

+3
source

namespace is redundant, as you explain. You can remove namespace { and match } .

One difference is that you can have different names ::TOKEN and unnamed_namespace::TOKEN . But this probably just adds to the confusion, and it would be better to get a compilation error.

Not sure if the second half of your message, the local TOKEN variable shadows both ::TOKEN and unnamed_namespace::TOKEN . Thus, the change will not make any difference to this case.

+5
source

Perhaps this improves clarity. The presence of an anonymous namespace says much more clearly: "this code is an implementation detail only for this compilation module, and not as part of the device interface." Since you must use anonymous namespaces for local classes or structures in any case (there is no other way to localize them), it is reasonable to encapsulate all your local constructs in this way.

0
source

All Articles