Search code examples
c++templatesstlgeneric-programming

Recursive generic function used as a predicate, compilation failure


I am writing a function to compare the contents of two lists. The order of the elements don't matter, so I sort them before I compare. The lists can be of normal types list<int>, but also be lists of lists list<list<int> >.

Here is a complete stripped down example:

#include <list>

template <typename T>
bool lessThanInAnyOrder(T lhs, T rhs)
{
  return lhs < rhs;
}

template <typename T>
bool lessThanInAnyOrder(std::list<T> lhs, std::list<T> rhs)
{
  lhs.sort(lessThanInAnyOrder<T>);
  rhs.sort(lessThanInAnyOrder<T>);

  //Do comparisons here, but for now just:
  return false;
}

int main()
{
  std::list<int> list1;
  std::list<int> list2;
  lessThanInAnyOrder(list1, list2);
}

This compiles in GCC 4.3.3, but in Visual Studio 2008, it gives the following compilation error where I'm calling lhs.sort():

error C2660: 'std::list<_Ty>::sort' : function does not take 1 arguments

Any suggestions?


Solution

  • Wrap the function in std::ptr_fun with explicit type arguments:

    lhs.sort(std::ptr_fun<T, T>(lessThanInAnyOrder<T>));