无法从函数参数的默认参数推断模板参数
Couldn't deduce template parameter from function parameter's default argument
我正在尝试创建一个函数,该函数在满足给定条件的范围内找到最小元素:
#include <functional>
#include <iostream>
#include <vector>
template <typename It, typename Pred, typename Comp>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
// Use less-than as the default comparator.
Comp comp = std::less<decltype(*std::declval<It>())>()
) {
It minElement = end;
for (It it = begin; it != end; ++it) {
if (!pred(*it)) {
continue;
}
if (comp(*it, *minElement)) {
minElement = it;
}
}
return minElement;
}
int main() {
std::vector<double> foo;
foo.push_back(6);
foo.push_back(10);
foo.push_back(-3);
foo.push_back(7);
std::cout << *minElementWhere(
foo.begin(),
foo.end(),
[](double val) {
return val >= 0;
}
) << std::endl;
}
但是我得到这个错误:
main.cpp: In function 'int main()':
main.cpp:40:5: error: no matching function for call to 'minElementWhere(std::vector<double>::iterator, std::vector<double>::iterator, main()::__lambda0)'
) << std::endl;
^
main.cpp:40:5: note: candidate is:
main.cpp:6:4: note: template<class It, class Pred, class Comp> It minElementWhere(It, It, Pred, Comp)
It minElementWhere(
^
main.cpp:6:4: note: template argument deduction/substitution failed:
main.cpp:40:5: note: couldn't deduce template parameter 'Comp'
) << std::endl;
Comp
不是返回类型,所以它没有试图推断返回类型,而且在我看来,Comp
似乎没有模棱两可的重载(因为只能有一种解引用It
的返回类型)。为什么我得到这个错误,我如何修复它?
您期望模板参数Comp
从您为相应的函数参数提供的默认参数中推导出来。然而,它被显式地列为非推导上下文,这意味着该模板形参的模板实参推导将失败(除非它可以从其他地方推导出来)。
未推断的上下文为:
-…
—A的参数类型中使用的模板参数中使用的默认实参的函数形参正在为其进行实参演绎的调用。
要成功演绎模板实参,需要为模板形参提供默认实参,而不是函数形参。
template <typename It,
typename Pred,
typename Comp = std::less<decltype(*std::declval<It>())>>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
) {
...
}
现场演示
您可能需要以下内容:
#include <iterator>
struct always_true{
template<typename T>
bool operator()(T&& val) const{
return true;
}
};
template <
typename It,
typename Pred = always_true,
typename Comp = std::less<typename std::iterator_traits<It>::value_type >
>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
){
It minElement = end;
for (It it = begin; it != end; ++it) {
if (pred(*it) && (minElement == end || comp(*it, *minElement))){
minElement = it;
}
}
return minElement;
}
这个适合我:
template <typename It, typename Pred,
typename Comp = std::less<decltype(*std::declval<It>())>>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
) {
It minElement = end;
for (It it = begin; it != end; ++it) {
if (!pred(*it)) {
continue;
}
if (comp(*it, *minElement)) {
minElement = it;
}
}
return minElement;
}
相关文章:
- 使函数参数默认为周围范围
- 如何将值传递给其参数(默认参数)
- 当“std::make_index_sequence”和“std::index_sequence”用于模板参数默认类型时
- C ,构造器中允许的参考参数默认值
- 哪些规则控制参数默认赋值?
- 错误:上一个规范后给出的参数默认参数
- 是否可以设计一个包含模板参数默认值的类
- 构造函数中参数C++默认值
- 如何设置依赖于其他参数的参数默认值
- C++模板参数默认函数实现
- QInput对话框参数默认值
- g++4.8.2上列表方法参数默认初始化时出错
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'
- 函数模板:将第一个模板参数默认为第二个
- 模板模板参数默认可以引用其他模板类型的参数
- 参数默认为先例参数的值
- 如何修改一个类,使它只有一个成员函数,所有参数默认
- 内置类型的模板参数默认值
- 是否可以使用构造函数或对象作为其他类方法的参数默认值?
- 模板参数默认为更高版本