Emmanuel Hadoux
Emmanuel Hadoux

Reputation: 70

C++14 Functional logical_not on functions

I have a logical expression, let's say !a & b. However, instead of evaluating this expression, I would like to build a functional tree and defer the evaluation to later. The values of a and b are computed using a function value so we have:

auto value_of_a = std::bind(value, _1); //I need an additional argument
auto value_of_b = std::bind(value, _1);

I would like to use logical_not and logical_and instead of using my own lambdas. However, they call a direct operator on the argument (for instance !arg for logical_not). Therefore I cannot use something like:

auto v = std::bind(logical_not, value_of_a); //And we still need the first argument 
                                             //to call value_of_a

Can I bind the result (like some sort of Future) instead of the function?

I am trying to use as much already defined functions as possible for the sake of readability. I am using C++14 but those are defined in C++11.

I am aware that using lambdas may be the easiest way, but I am trying to leverage as much as I can what already exists. However, I will fall back to them if the solution is not really better.

Thanks, hope it is clear enough.

Upvotes: 3

Views: 392

Answers (1)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48507

std::bind(std::logical_and<>{}, 
              std::bind(std::logical_not<>{}, value_of_a),
              std::bind(value_of_b, std::placeholders::_2));

Provided that value_of_a is a bind expression, the above code is equivalent to:

( not value_of_a(#1) ) and ( value_of_b(#2) )

DEMO

Upvotes: 3

Related Questions