J
James W. Walker
Can someone please explain to me why the following code gives a compile
error when instantiating the sort algorithm?
#include <vector>
namespace
{
struct Foo {
int m;
};
}
static bool operator<( const Foo& inOne, const Foo& inTwo )
{
return inOne.m < inTwo.m;
}
static void test()
{
std::vector<Foo> vec;
std::sort( vec.begin(), vec.end() );
}
It's as if it doesn't understand that I have provided operator< for
Foo. But if I just say
Foo x,y; if (x < y) {... }
instead of sorting, there is no problem. If I move the operator< into
the namespace, then it compiles. So, I know how to fix it, but I'd
like to understand it.
error when instantiating the sort algorithm?
#include <vector>
namespace
{
struct Foo {
int m;
};
}
static bool operator<( const Foo& inOne, const Foo& inTwo )
{
return inOne.m < inTwo.m;
}
static void test()
{
std::vector<Foo> vec;
std::sort( vec.begin(), vec.end() );
}
It's as if it doesn't understand that I have provided operator< for
Foo. But if I just say
Foo x,y; if (x < y) {... }
instead of sorting, there is no problem. If I move the operator< into
the namespace, then it compiles. So, I know how to fix it, but I'd
like to understand it.