Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnamed/anonymous namespaces vs. static functions

Tags:

c++

namespaces

A feature of C++ is the ability to create unnamed (anonymous) namespaces, like so:

namespace {     int cannotAccessOutsideThisFile() { ... } } // namespace 

You would think that such a feature would be useless -- since you can't specify the name of the namespace, it's impossible to access anything within it from outside. But these unnamed namespaces are accessible within the file they're created in, as if you had an implicit using-clause to them.

My question is, why or when would this be preferable to using static functions? Or are they essentially two ways of doing the exact same thing?

like image 305
Head Geek Avatar asked Sep 30 '08 19:09

Head Geek


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.

Are variables in anonymous namespace static?

Theoretically, extern variables declared in anonymous namespace is a superior alternative over a simple static variable. That's why the "static global variables" were deprecated before C++11.

What is an anonymous namespace C++?

A namespace with no identifier before an opening brace produces an unnamed namespace. Each translation unit may contain its own unique unnamed namespace. The following example demonstrates how unnamed namespaces are useful.

What does static keyword do in C++?

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name. Static variables are initialized only once. Compiler persist the variable till the end of the program.


2 Answers

The C++ Standard reads in section 7.3.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.

Edit:

The decision to deprecate this use of the static keyword (affecting visibility of a variable declaration in a translation unit) has been reversed (ref). In this case using a static or an unnamed namespace are back to being essentially two ways of doing the exact same thing. For more discussion please see this SO question.

Unnamed namespace's still have the advantage of allowing you to define translation-unit-local types. Please see this SO question for more details.

Credit goes to Mike Percy for bringing this to my attention.

like image 166
luke Avatar answered Oct 06 '22 23:10

luke


Putting methods in an anonymous namespace prevents you from accidentally violating the One Definition Rule, allowing you to never worry about naming your helper methods the same as some other method you may link in.

And, as pointed out by luke, anonymous namespaces are preferred by the standard over static members.

like image 32
hazzen Avatar answered Oct 07 '22 00:10

hazzen