The first thing to be noted is that an anonymous namespace
is not the same as no namespace
.
namespace
{
bool operator==(const Foo&, const Foo&) {...}
}
is really something like
namespace ANameUniqueToTheFile
{
bool operator==(const Foo&, const Foo&) {...}
}
using ANameUniqueToTheFile;
with the downside that the language does not give you the ability to get the name of the namespace
.
For that reason, the operator==
function defined in the anonymous namespace
is not found using ADL.
I can see why you would want to put the operator==
function in a namespace
. One possbile way to do that would be to #include
the .h file that defines the struct
inside a named namespace
.
FooWrapper.h:
namespace MyApp
{
#include "foo.h"
// Declare the function.
bool operator==(Foo const& lhs, Foo const& rhs);
}
FooWrapper.cpp
#include "FooWrapper.h"
namespace MyApp
{
// Implement the function.
bool operator==(Foo const& lhs, Foo const& rhs) { ... }
}
However...
I realize that I'm simplifying a bit about what's in "foo.h". Adding all of them in namesapce MyApp
might not be appropriate, specially if "foo.h" includes other .h files and/or standard header files. I am hoping it gives you some ideas on how to tackle the problem.
If putting all of "foo.h" in namespace MyApp
does not work smoothly, it will probably be expedient to define the operato==
function in global scope.