62

Possible Duplicate:
Unnamed/anonymous namespaces vs. static functions

I came across this code

namespace ABC {
namespace DEF {

namespace
{

I expected the namespace should be followed by some name, but it's not the case with this code.

Is this allowed in C++? What's the advantage for this unnamed namespace?

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 4
    Dup of [Unnamed/anonymous namespaces vs. static functions](http://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions) – Matt Ball Mar 08 '11 at 22:09
  • 7
    It's not an exact duplicate. This question asks "What is X?" The other question asks "When should we use X instead of Y?" – Rob Kennedy Mar 09 '11 at 00:28

1 Answers1

101

It's called an unnamed namespace / anonymous namespace. It's use is to make functions/objects/etc accessible only within that file. It's almost the same as static in C.

Marlon
  • 19,924
  • 12
  • 70
  • 101
  • 6
    Except that `namespace { void foo() { } }` in one file, `#include` it from another and try to call `foo();` works. It's not accessible only within that file. It means, that the stuff in it has internal linkage. I'd say that's a pretty critical oversight in the answer. – Niko O May 17 '22 at 05:26