Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are unnamed namespaces used and what are their benefits?

I just joined a new C++ software project and I'm trying to understand the design. The project makes frequent use of unnamed namespaces. For example, something like this may occur in a class definition file:

// newusertype.cc namespace {   const int SIZE_OF_ARRAY_X;   const int SIZE_OF_ARRAY_Y;   bool getState(userType*,otherUserType*); }  newusertype::newusertype(...) {... 

What are the design considerations that might cause one to use an unnamed namespace? What are the advantages and disadvantages?

like image 223
Scottie T Avatar asked Dec 10 '08 20:12

Scottie T


People also ask

Why do we use unnamed namespace?

Unnamed Namespaces They are directly usable in the same program and are used for declaring unique identifiers.

Why is an unnamed namespace used instead of static?

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.

What does an anonymous namespace do C++?

2)We can have anonymous namespaces (namespace with no name). They are directly usable in the same program and are used for declaring unique identifiers. It also avoids making global static variable. The “anonymous” namespace you have created will only be accessible within the file you created it in.

What is using namespace std in C++ Geeksforgeeks?

Namespace is a feature added in C++ and is not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it. Multiple namespace blocks with the same name are allowed.


1 Answers

Unnamed namespaces are a utility to make an identifier translation unit local. They behave as if you would choose a unique name per translation unit for a namespace:

namespace unique { /* empty */ } using namespace unique; namespace unique { /* namespace body. stuff in here */ } 

The extra step using the empty body is important, so you can already refer within the namespace body to identifiers like ::name that are defined in that namespace, since the using directive already took place.

This means you can have free functions called (for example) help that can exist in multiple translation units, and they won't clash at link time. The effect is almost identical to using the static keyword used in C which you can put in in the declaration of identifiers. Unnamed namespaces are a superior alternative, being able to even make a type translation unit local.

namespace { int a1; } static int a2; 

Both a's are translation unit local and won't clash at link time. But the difference is that the a1 in the anonymous namespace gets a unique name.

Read the excellent article at comeau-computing Why is an unnamed namespace used instead of static? (Archive.org mirror).

like image 174
Johannes Schaub - litb Avatar answered Oct 23 '22 08:10

Johannes Schaub - litb