Namespaces and Operator Overloading in C++

Solution 1:

You should define them in the library namespace. The compiler will find them anyway through argument dependant lookup.

No need to pollute the global namespace.

Solution 2:

Putting it into the library namespace works because of Koenig lookup.

Solution 3:

You should define it in the namespace, both because the syntax will be less verbose and not to clutter the global namespace.

Actually, if you define your overloads in your class definition, this becomes a moot question:

namespace Lib {

class A {
public:
    A operator+(const A&);
};

} // namespace Lib