For example, c++ std::find_if is implemented like:
template <class InputIter, class UnaryPredicate>
InputIter find_if(InputIter begin, InputIter end, UnaryPredicate pred) {
for (; begin != end; begin++) {
if (pred(*begin))
return begin;
}
return end;
}
InputItershould bestd::input_iterator_tagtype, but in this function, any type which hasoperator++operator++(int)can be compiled.UnaryPredicateshould be something likebool function(const T &value), but any function returnintdoublecan be compiled.
Is STL template parameter precise enough ? It seems doesn't check all possible error when compiling.
Can we implement it like:
template <T, Iter<? super std::input_iterator<T>>, Pred<? implement bool (const T&)>>
Iter find_if(Iter begin, Iter end, Pred p) {
for (; begin != end; begin++) {
if (p(*begin))
return begin;
}
return end;
}
In this fake code, I want find_if work for type T, it has range in [begin, end), and use bool (const T &) to check if it is statisfied ?
Aucun commentaire:
Enregistrer un commentaire