Joshua MN
Joshua MN

Reputation: 1526

Anonymous namespace - has sense in both headers and code files?

test.cpp

#include "test.hpp"

void f1() { }

namespace {
   void f2() {}
}

namespace test {
   void f3() { }
}

Please correct me if I'm wrong, but:

Am I right?

Upvotes: 2

Views: 3814

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 72063

Your question title is misleading. Don't put an anonymous namespace in a header, ever.

f2 can't be called from outside of test.cpp, correct.

f1 and f3 can be called if appropriate declarations are available. These can be in a header, but nothing stops other .cpp files from simply having the code that would be in a header, even if you don't provide one.

Also, extern is not needed for function declarations.

void f1();
namespace test {
  void f3();
}

Upvotes: 3

Related Questions