问题
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?
回答1:
You can create a macro like
#define FUNCTORIZE(func) [](auto&&... val) \
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
which will let you wrap any callable into a closure object. You would use it like
auto constexpr predObj = FUNCTORIZE(pred);
来源:https://stackoverflow.com/questions/65811716/most-terse-and-reusable-way-of-wrapping-template-or-overloaded-functions-in-func