Combine predicates with logical conjunction
This example wraps a lambda in a function to create a custom conjunction for use with an algorithm predicate.
How to do it…
The copy_if()
algorithm requires a predicate that takes one parameter. In this recipe, we will create a predicate lambda from three other lambdas:
- First, we'll write the
combine()
function. This function returns a lambda for use with thecopy_if()
algorithm:template <typename F, typename A, typename B> auto combine(F binary_func, A a, B b) { return [=](auto param) { return binary_func(a(param), b(param)); }; }
The combine()
function takes three function parameters – a binary conjunction and two predicates – and returns a lambda that calls the conjunction with the two predicates.
- In the
main()
function, we create the lambdas for use withcombine()
:int main() { ...