In C++ stl, there is e.g.:
template<class InputIt, class UnaryPredicate>
constexpr InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
My question is: why there is not:
... find_if(..., UnaryPredicate &&p);
In my understanding it would be more "universal". R-value predicates still would be passed by value. Additionally, L-value predicates could be passed too:
struct MyComplexPred {
MyComplexPred(MyComplexPred const &) = delete; // expensive/"impossible" copy
MyComplexPred() { /*...*/ }
bool operator()(int val) const { return true; /*...*/ }
};
int fun() {
MyComplexPred pred;
std::vector<int> a, b;
std::find_if(a.begin(), a.end(), pred); // impossible, no copy
std::find_if(b.begin(), b.end(), pred); // impossible, reuse predicate
...
I found a similar question here but I wonder why the suggested &&-passing with reference collapsing is not used in stl for predicates and other function objects.