Sto cercando di fare una funzione che trova l'elemento minimo in una gamma che soddisfa una determinata condizione:Impossibile dedurre dei parametri modello dal argomento di default del parametro di funzione
#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;
}
ma ottengo questo errore:
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
non è il tipo di ritorno, in modo che non sta cercando di dedurre il tipo di ritorno, e non mi pare che ci siano sovraccarichi ambigue di Comp
(dal momento che non ci può essere un solo tipo di ritorno di dereferenziazione uno It
). Perché ricevo questo errore e come posso risolverlo?
Non è possibile dedurre parametri di modello da argomenti in default. – ghostofstandardspast
[OT]: la tua implementazione è UB come fai 'comp (* it, * end)'. – Jarod42