[Solved] std::sort() does not look for global operator


You are not providing enough information. However, an educated guess would be that your actual code involves namespaces and you are experiencing an ordinary ADL (argument-dependent lookup) failure.

If the type stored in v is not a member of global namespace, then the compiler is not supposed to unconditionally search the entire global namespace for comparison operator.

The compiler will perform regular lookup for operator < from the point of definition of std::sort, which obviously will not find your global operator < (since std::sort is defined somewhere in one of the standard library files).

The compiler will also perform ADL in namespaces associated with the type being compared. You are not telling us much about that type, but I’d guess that that type is not defined in the global namespace. For this reason global namespace is not nominated for ADL and the operator is not found.

Here a simple example that won’t compile for the very same reason

#include <iterator>
#include <algorithm>

namespace N {
  struct S { int i = 0; };
}

bool operator <(const N::S &lhs, const N::S &rhs)
{
  return lhs.i < rhs.i;
}

int main()
{
  N::S a[10];
  std::sort(std::begin(a), std::end(a));
}

The compiler is not supposed to find the global operator < even though it is right there in plain sight.

1

solved std::sort() does not look for global operator<() defined