70

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?

2
  • 4
    Dup of Unnamed/anonymous namespaces vs. static functions Commented Mar 8, 2011 at 22:09
  • 8
    It's not an exact duplicate. This question asks "What is X?" The other question asks "When should we use X instead of Y?" Commented Mar 9, 2011 at 0:28

1 Answer 1

104

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.