我在 C++ 中定义了以下函数:
template<class Type> Type GetMedian(const vector<Type>& items, function<bool(Type, Type)> comp) {
vector<Type> copied_items(items);
std::nth_element(copied_items.begin(), copied_items.begin() + copied_items.size()/2, copied_items.end(), comp);
return copied_items[copied_items.size()/2];
}
但是,当我尝试将其称为 GetMedian(v, greater<uint32_t>())
时,我的编译器 (clang) 提示:
error: no
matching function for call to 'GetMedian'
GetMedian(v, greater<uint32_t>());
^~~~~~~~~
note:
candidate template ignored: could not match 'function' against 'greater'
template<class Type> Type GetMedian(const vector<Type>& items, function...
但是,每当我更改为不使用模板时,我都没有看到此错误,因为:
uint32_t GetMedian(const vector<uint32_t>& items, function<bool(uint32_t, uint32_t)> comp) {
vector<uint32_t> copied_items(items);
std::nth_element(copied_items.begin(), copied_items.begin() + copied_items.size()/2, copied_items.end(), comp);
return copied_items[copied_items.size()/2];
}
有没有办法让我的函数像我想的那样灵活?
最佳答案
类型Type
在这里有两个地方推导:
template<class Type>
Type GetMedian(const vector<Type>& items, function<bool(Type, Type)> comp);
^^^^ ^^^^^^^^^^
当您使用 GetMedian(v, greater<uint32_t>())
调用它时, 它会推导出 Type
作为uint32_t
对于 v
, 但随后需要推导 function<bool(Type, Type)>
反对greater<uin32_t>
.但后者不是 function
类型,所以推导失败。它可转换为function<bool(uint32_t, uint32_t)>
,但在模板推导过程中不会发生转换。
幸运的是,您实际上并不需要 std::function
这里。拥有它实际上更糟糕——你无缘无故地给自己带来了类型删除的开销。只需让比较器成为一个单独的模板类型即可:
template <class Type, class Comp>
Type GetMedian(const vector<Type>& items, Comp comp);
或者,如果您真的非常想要一个 std::function
,你可以包装 Type
在非推导的上下文中通过类似的东西:
template <class T> struct non_deduced { using type = T; };
template <class T> using non_deduced_t = typename non_deduced<T>::type;
template <class T>
T median(const std::vector<T>&, std::function<bool(non_deduced_t<T>, non_deduced_t<T>)>)
现在,来自 std::greater<uint32_t>
的转换至 std::function<bool(uint32_t, uint32_t)>
允许发生,因为它只是 vector<T>
这是一个推导出的上下文,所以编译器推导出 T
至 uint32_t
然后检查第二个参数转换是否有效。
关于c++ - 具有函数类型参数的模板导致编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35872940/