无法从函数参数的默认参数中推导出模板参数

时间:2014-06-18 05:53:16

标签: c++ templates c++11

我正在尝试创建一个函数,该函数在满足给定条件的范围内找到最小元素:

#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的返回类型。为什么我会收到此错误,我该如何解决?

3 个答案:

答案 0 :(得分:8)

您期望从您为相应函数参数提供的默认参数推导出模板参数Comp。但是,这被明确列为非推导上下文,这意味着模板参数推导将失败该模板参数(除非它可以从其他地方推断出来。)

来自§14.8.2.5/ 5 [temp.deduct.type]

  

未推断的背景是:
   - ......
   - 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()
) {
...
}

Live demo

答案 1 :(得分:2)

您可能需要以下内容:

#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;
}

答案 2 :(得分:1)

这对我有用:

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;
}
相关问题