Scenario 1: a template function pred
template<typename T>
bool pred(T t) { /* return a bool based on t */ }
Scenario 2: a set of functions overloaded on the same name pred
bool pred(A t) { /* return a bool based on t */ }
bool pred(B t) { /* return a bool based on t */ }
bool pred(C t) { /* return a bool based on t */ }
...
Whichever of the two scenarii we're in, the bottom line is that pred
does not refer to a function, and so it cannot be passed around, e.g. as a unary predicate to std::remove_if
.
Therefore it is convenient in this case to define the following object which can be passed around instead,
auto constexpr predObj = [](auto t){ return pred(t); };
However, as soon as I have a similar need for another unary predicate, I need to copy and paste that line and change the two names to something else; similarly if I need to do that for a binary predicate:
auto contexpr binPredObj = [](auto x, auto y){ return binPred(x, y); };
Is there a general way of making this automatically? I'm thinking of something like
auto funObj = fun2Obj(fun);
I have the feeling that what I ask is not possible exactly because it would require passing fun
as it was a function object, which it isn't, otherwise I wouldn't need to make a function object out of it. But asking is never crime, right?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…