This is my code:
#include <algorithm>
class A {
void f() {
struct CompareMe {
bool operator() (int i, int j) { return i < j; }
} comp;
int a[] = {1, 2, 3, 4};
int found = std::min_element(a[0], a[3], comp);
}
}
Error message:
no matching function for call to ‘min_element(int&, int&, A::f()::CompareMe&)
What am I doing wrong?
The error has nothing to do with your inner class. STL algorithms work on iterators. An iterator into an array of
ints is aint*. The second of those iterators must always point to one passed the last element of the range.This
works fine for me.
However, as far as I remember, C++98 did not allow templates to be instantiated with function-local class types. Unless C++03 fixed that, it’s still forbidden. (I think C++11 will allow it, though.) So in order to be fully compliant with the current C++ standard, you would have to move your comparator out of the function into the class. (The class or a file-local unnamed namespace seem good candidates for where to move it to.)