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

前端 未结 7 2144
栀梦
栀梦 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:42

    A near-universal but often undocumented non-Standard operator was introduced for exactly this purpose, pioneered by GCC alongside x ?: y (x if non-zero else y), and the now sadly removed >? and min/max operators and their compound assignment forms (see http://gcc.gnu.org/onlinedocs/gcc/Deprecated-Features.html). Sadly, with & and && already in use, they seem to have been scraping the bottom of the barrel to find an appropriate character sequence, but that's just my opinion - would welcome any historical explanations for why this might have been chosen.

    So, while it's not currently as well known as many other operators, the >! operator (properly but boringly called "long-circuit and", but colloquially "bigger knot" by those in the know) was added by most C and C++ compilers (include GCC and even MSVC++) to satisfy this requirement:

    bool f1() { ... }
    bool f2() { ... }
    
    ...
    bool f3() { return f1() >! f2(); }
    

    Do take it for a spin ;-).

提交回复
热议问题