简体   繁体   English

C ++ 14函数logical_not on functions

[英]C++14 Functional logical_not on functions

I have a logical expression, let's say !a & b . 我有一个逻辑表达式,比方说吧!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: 使用函数value计算abvalue因此我们得到:

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. 我想使用logical_notlogical_and而不是使用我自己的lambdas。 However, they call a direct operator on the argument (for instance !arg for logical_not ). 但是,它们在参数上调用直接运算符(例如!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? 我可以绑定结果(比如某种Future)而不是函数吗?

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. 我使用的是C ++ 14,但这些是在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. 我知道使用lambdas可能是最简单的方法,但我试图充分利用已经存在的内容。 However, I will fall back to them if the solution is not really better. 但是,如果解决方案不是更好的话,我会回到他们身边。

Thanks, hope it is clear enough. 谢谢,希望它足够清楚。

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: 如果value_of_a是绑定表达式,则上述代码等效于:

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM