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

约书亚·班布里克(Joshua Bambrick)

我在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>()),它就会演绎Typeuint32_tv,但后来它需要推断function<bool(Type, Type)>反对greater<uin32_t>但是后者不是type function,因此推论失败。它可以转换function<bool(uint32_t, uint32_t)>,但是在模板推导过程中不会进行转换。

幸运的是,您实际上不需要std::function这里。拥有它实际上更糟-您无缘无故地给自己分配了类型擦除的开销。只是让比较器是一个单独的模板类型:

template <class Type, class Comp>
Type GetMedian(const vector<Type>& items, Comp comp);

或者,如果您真的很想要a 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,然后检查第二个参数转换是否有效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

具有可变参数模板的模板元编程:编译器错误

具有actionBlock的宏会导致编译器错误

模板模板参数会导致Clang下的编译器错误,但不会导致GCC

具有重载功能的编译器错误

确保模板功能的编译器错误

模板模板参数和模板别名:编译器错误?

使用自定义比较器功能作为模板参数的编译器错误

声明具有类型定义的返回类型的好友函数时,编译器错误

声明具有类型定义的返回类型的好友函数时,编译器错误

如何使编译器检查2个方法参数是否具有相同类型?

具有动态函数参数的C#编译器类型推断

具有模板非类型模板参数的功能模板

关于私有功能@doc的沉默编译器警告

对具有默认参数值的函数使用通配符模式会引发编译器254错误

Java编译器错误难题:“内部类不能具有静态声明”-简单类型除外

具有功能参数的对象破坏

具有功能参数的CoreData谓词

具有功能的管道的参数化

Typescript具有功能的联合类型

错误的重载导致编译器错误

为什么不以不同单位调用具有相同签名的函数会导致编译器错误?

具有功能的BTable筛选器

具有类名antbuild的Javac编译器错误

编译器推断模板参数

传递默认模板类型参数时,编译器选择模板专业化

DataBinding导致Kotlin编译器错误

具有功能的Typescript接口。子类型不能作为参数用于实现接口

引用泛型派生类仅指定一些类型参数会导致编译器出现“不兼容类型”错误

本地设置的Cython编译器指令会影响一个或所有功能吗?