I think I finally found where the predicate function is called:
02076 /// This is a helper function for the sort routine.
02077 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
02078 void
02079 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
02080 _Compare __comp)
02081 {
02082 _RandomAccessIterator __next = __last;
02083 --__next;
02084 while (__comp(__val, *__next))
02085 {
02086 *__last = *__next;
02087 __last = __next;
02088 --__next;
02089 }
02090 *__last = __val;
02091 }
That sort function does not take a template type. Once the SomeContainer template is instantiated with the type int (by making an object of type SomeContainer<int>), its sort function is defined to take a function pointer of type bool(int const &, int const &) and nothing else. That is different from std::list::sort which in itself is a template function inside the template class std::list.
Btw you mean that std::list::sort cannot exhibit the same behaviour as my sort function from the example because std::list::sort is a template function - did I get it right? Is it just because std::list::sort is a template function, or is there something more to it?
Thanks a lot for all the advice!
/Edit:
If default template arguments for function templates were actually available(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226), I could do this:
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T>
class someContainer
{
private:
T val1;
T val2;
public:
someContainer(const T& in1, const T& in2)
:val1(in1), val2(in2) {}
template <typename R = T>
void sort(bool (*_comp)(const R&, const R&))
{
cout << "Comp is of type: " << typeid(_comp).name() << endl;
cout << _comp(val1, val2) << endl;
return;
}
};
template <typename T>
bool compare(const T& a, const T& b)
{
return a>b;
}
int main()
{
someContainer<int> myCont(5,6);
myCont.sort(compare);
cin.ignore();
return 0;
}
However for now, only this variant works:
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T>
class someContainer
{
private:
T val1;
T val2;
public:
someContainer(const T& in1, const T& in2)
:val1(in1), val2(in2) {}
template <typename R>
void sort(bool (*_comp)(const R&, const R&))
{
cout << "Comp is of type: " << typeid(_comp).name() << endl;
cout << _comp(val1, val2) << endl;
return;
}
};
template <typename T>
bool compare(const T& a, const T& b)
{
return a>b;
}
int main()
{
someContainer<int> myCont(5,6);
myCont.sort(compare<int>);
cin.ignore();
return 0;
}