我有一个声明如下的函数;它的确切工作与此无关。
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);
在呼叫站点,我尝试执行以下操作:
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;
}
对于partition3
编译器的两次调用(MSVC 2010)抱怨它无法推断出最后一个参数的模板参数。如果我替换cmp()
为F
,则代码可以编译并正常工作。
我有两个问题:
- 为什么我会收到错误消息?[编译器错误或一些神秘的 C++ 规则?]
- 如果不首先明确构造,我怎样才能达到相同的效果
F
?
(目前,我已经通过引入另一个模板参数partition3
并声明P
为该模板类型来解决该问题。)