Search code examples
c++booleanoperatorsin-place

in-place bool AND and OR in c++?


Is there something like in-place AND and OR operators for bools in c++, along this line?

bool someOk=false;
for(int i=0; i<10; i++){
  someOk||=funcReturningBoolOnSuccess(i);
}

(I know I can write someOk=someOk||funcReturningBoolOnSuccess(i), but it is not as pretty).


Solution

  • The answer will be short: no, the C++ syntax does not allow such structure.

    You have to use:

    something = something || something_else;
    

    However.... if your function returns bool on success (as in, true on success)... Why don't you just use the following?

    someOk = funcReturningBoolOnSuccess(i);
    

    Will it not return false on failure anyway? Seems illogical.

    Why don't you just do:

    bool someOk=false;
    for(int i=0; i<10; i++){
      if (funcReturningBoolOnSuccess(i)) {
          someOk = true;
          break;
      }
    }
    

    A lot more efficient :)