C++ logical & operator

前端 未结 4 1796
不知归路
不知归路 2021-01-17 22:03

Is there a logical & operator in C++? e.g. an operator that works just as && except that it also evaluates later arguments even if some preceding ones have alrea

4条回答
  •  天涯浪人
    2021-01-17 22:42

    If you overload the operator &&, it won't short circuit.

    struct Bool {
      bool val;
      Bool(bool f): val(f) {}
      operator bool() {
        return val;    
      }
    };
    
    
    bool operator&&(Bool a, Bool b) {
      return (bool)a && (bool)b;
    }
    

    ref: point 19, section 13.9 in the C++ FAQ lite

    Though as mentioned there, this is a very bad idea and confuses people. You might want to do it in a very limited way though if you have a very special case.

提交回复
热议问题