The following code snippet defines two binary predicates Cmp1 and Cmp2 for sorting. Cmp1 is templated, and Cmp2 is just a special case with T == int*. The code compiles (using gcc) with the second std::sort commented out, but not with it included, though the two should be identical. (I know that the memory between b and b_end isn't initialized; it doesn't matter, that's not where the error is coming from.) The problem seems to be that sort() doesn't like a comparison function with explicit pointer types, even though it's perfectly valid, so one has to sneak them in using a template. Any ideas why?
Code:#include <algorithm> template<class T> class Cmp1 { public: bool operator() (const T &x1, const T &x2) const { return true; } }; class Cmp2 { public: bool operator() (const int* &x1, const int* &x2) const { return true; } }; int main() { int **b, **b_end; std::sort(b, b_end, Cmp1<int*>()); // std::sort(b, b_end, Cmp2()); }



LinkBack URL
About LinkBacks



CornedBee