Unnamed/anonymous namespaces vs. static functions

Posted by Head Geek on Stack Overflow See other posts from Stack Overflow or by Head Geek
Published on 2008-09-30T19:02:00Z Indexed on 2010/04/16 4:33 UTC
Read the original article Hit count: 217

Filed under:
|

A little-used feature of C++ is the ability to create 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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about namespaces