具有功能类型的模板参数原因编译器错误

Jos*_*ick 3 c++ generics lambda templates

我在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];
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将其称为时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...
Run Code Online (Sandbox Code Playgroud)

但是,每当我更改为不使用模板时,我都没有看到此错误,如:

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];
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让我的功能像我想的那样灵活?

Bar*_*rry 7

这个类型Type在这里有两个点:

template<class Type> 
Type GetMedian(const vector<Type>& items, function<bool(Type, Type)> comp);
                            ^^^^                        ^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

当你调用它GetMedian(v, greater<uint32_t>()),它就会演绎Typeuint32_tv,但后来它需要推断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);
Run Code Online (Sandbox Code Playgroud)

或者,如果你真的真的想要一个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>)>)
Run Code Online (Sandbox Code Playgroud)

现在,从转换std::greater<uint32_t>std::function<bool(uint32_t, uint32_t)>被允许发生,因为这仅仅是vector<T>这是一个推断的上下文,因此编译器推导Tuint32_t,然后检查第二个参数的转换工作.