4
//file.h

namespace Foo{
namespace{
   void func(){}
}
}

vs

namespace Foo{ 
   void func(){} 
}

//file2.cpp use file.h's method

What is the consequence (if there is one) in the calling code (in term of visiblity for example) between this two approach ?

Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
  • 1
    You should not use unnamed namespaces in header files: http://stackoverflow.com/q/7955058/1025391 – moooeeeep Jun 30 '14 at 07:45
  • 1
    Ummm I am not sure what do you mean by the "consequences of calling the code". As far as I know, consequences of calling a particular code are always the same, regardless of which namespace it lies in. Namespaces limit only visibility and locality of what is declared within them. – luk32 Jun 30 '14 at 07:45

3 Answers3

6

This:

namespace Foo {
namespace {

void func() {}

}
}

Is largely equivalent to this:

namespace Foo {

static void func() {}

}

The difference is that in the static case, the function has internal linkage, so it is not visible to the linker. In the unnamed namespace case, the function has external linkage (is visible to the linker), but under a name which none of your other source files can "normally" access. You might still call the function from a different source file if you reverse-engineer the compiler's name mangling scheme, and the function is still listed among the object file's symbols, for example.

But the common point is that each source file which includes the code (perhaps by #includeing the header file) will contain its own copy of the function. This can have an impact on the size of your binary.

Also, if you need the first one for some reason, you should heavily document it. An unnamed namespace in a header file is generally a "WTF" point, and you do not want these in your code. I must say I can't think of a viable use case for this.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

Both variants will allow the function to be found under the name Foo::func().

The compiler might generate different code in the two cases though. Declaring a function inside an anonymous namespace makes that function local to the .cpp file. That is, every .cpp file that includes the header might end up with their own (identical) instantiation of the code for func. This might lead to some bloat in the final executable, due to the duplicated code.

Note that if you define the function inline (as is suggested by your question) this is not really an issue, as the code will be duplicated anyway due to inlining.

Still, as pointed out in the comments: Anonymous namespaces in headers are unusual and will draw the suspicion of however reviews this code. You should always prefer the second option, unless you have very good reason not to.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • So the side effects will be the same, just the actual binary code produced will be duplicated, as if the function were declared `inline` or `static`. Where for the latter unnamed namespace is actually a superior method of doing it c++. – luk32 Jun 30 '14 at 07:56
1

The first is the equivalent of:

namespace Foo
{
    namespace TranslationUnitSpecific
    {
        void func();
    }
}

It means that every time you include the header, you declare a new, unrelated instance of func. If func is not inline, you would have to define it in every source file that uses it. (On the other hand, it does mean that you can provide an implementation in the header without making the function inline.)

It also means that you cannot use the function in inline or template functions defined in a header with risking undefined behavior, due to violations of the one definition rule.

In general, there are very few, if any cases where you should use an unnamed namespace in a header.

James Kanze
  • 150,581
  • 18
  • 184
  • 329