I have a function declared as follows; its exact working is not relevant for this.
template<typename T>
std::pair<int, int>
partition3(T *pT, const int N, const T &Kq, const int w,
std::function<int(const T&, const T&, int)> P);
At the call site I attempt to do the following:
bool partition3_test()
{
struct cmp
{
int operator()(int x, int y, int) const
{ return x-y; }
};
int V1[11] = { 3, 7, 1, 7, 7, 8, 10, 2, 16, 4, 3 },
V2[11] = { 3, 6, 1, 6, 6, 8, 10, 2, 16, 4, 3 };
std::function<int(const int&, const int&, int)> F = cmp();
std::pair<int, int>
p1 = partition3(V1, 11, 7, 0, cmp()),
p2 = partition3(V2, 11, 7, 0, cmp());
return false;
}
For the two calls of partition3
the compiler (MSVC 2010) complains that it could not deduce template argument for the last parameter. If I replace cmp()
with F
, the code compiles and works fine.
I have two questions:
- Why do I get the error? [compiler bug or some arcane C++ rule?]
- How can I achieve the same effect without first explicitly constructing
F
?
(Currently, I have solved the problem by introducing another template parameter on partition3
and declaring P
as that template type.)