c++ - 具有函数类型参数的模板导致编译器错误

标签 c++ templates generics lambda

我在 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>这是一个推导出的上下文,所以编译器推导出 Tuint32_t然后检查第二个参数转换是否有效。

关于c++ - 具有函数类型参数的模板导致编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35872940/

相关文章:

c++ - 为什么不区分大小写与 std::unordered_set 的 std::hash 函数一起使用?

c++ - tinyXML 库无法正确读取 ‘&’

C++ - 获取派生类型作为模板参数

c++ - 有没有办法像 C++ 中的 printf 那样用参数替换字符串?

C++ 函数重载,enable_if 的替代品?

c++ - typedef 带有参数的模板化类

java - 使用字符串的通用二分搜索

java - 如何使用 Java 泛型以便为返回类型为 List<Interface> 的方法返回 List<InterfaceImpl>

java - Java 中数组与集合的运行时安全性

c++ - 如何阅读我的套接字消息