Is there an Non-Short circuited logical “and” in C++?

前端 未结 7 2155
栀梦
栀梦 2020-12-06 08:49

tl;dr: Is there a non-short circuit logical AND in C++ (similar to &&)?

I\'ve got 2 functions that I want to call, and use the return values to figure out th

7条回答
  •  孤城傲影
    2020-12-06 09:51

    You can trivially write your own.

    bool LongCircuitAnd( bool term1, bool term2 ) { return term1 && term2; }
    
    bool Func3(int x, int y, int z, int q){
      return LongCircuitAnd( Func1(x,y), Func2(z,q) ); 
    

    And if you want to be very fancy, you could even inline it!!!

    Okay, Okay, if you really really don't want the terrible overhead of calling a function.

    bool Func3(int x, int y, int z, int q){
      return ((int)Func1(x,y)) * ((int)Func2(z,q)); 
    

    But I don't consider that elegant. It its conceivable that an overly smart compiler could short circuit this...

提交回复
热议问题