There are a number of things to say:
What you defined is a function template, not a class template. Since you are using a default template parameter
typename Compare = std::less<Type>
I presume you are already using C++11, because for all I know function templates did not allow default template parameters in the previous versions of the standard.
On the other hand, default arguments of template parameters like this
Compare comp = Compare()
were possible in the previous version of the standard, too. Your statement that default arguments are not possible for templated parameters is wrong (or perhaps it actually referred to what I called default template parameters above).
The compiler error messages that you receive must be due to some other problem. Perhaps the Type you end up using does not go well with std::less, or the Compare type you use does not implement the default constructor. In any case, the following program compiles on GCC 4.6.2 (note that I changed the std::vector<> & to const std::vector<> & because that seemed more right):
#include <vector>
#include <functional>
#include <algorithm>
template <typename Type, typename Compare = std::less<Type> >
Type FindMax(const std::vector<Type> &vec, Compare comp = Compare()) {
return *std::max_element(vec.begin(),vec.end(),comp);
}
int main() {
FindMax(std::vector<int>());
return 0;
}
And indeed this requires the -std=C++0x option, but that is because the default template parameter, not the default argument.
About the extra question related to cmpFn:
That declares a function parameter, i.e. an argument that is itself a function. The declaration
int (cmpFn)(ElemType, ElemType)
means the local name of the function is cmpFn, its return type is int, and it takes two arguments, both of type ElemType. The idea is that the caller can pass a function (or a functor) that will then be used to compare the elements of the vector. E.g. if you define the default value of that argument OperatorCmp before the function declaration like this:
int OperatorCmp(int a, int b) {
return (a<b?-1:(a>b?1:0));
}
the declaration becomes valid and you can use it to find the maximum value of a std::vector<int>.