From cppreference.com
Returns the result of a boolean operation.
Operator name Syntax
Prototype examples (for classT) Inside class definition Outside class definition negation nota!a
Yes boolT::operator!()const;booloperator!(constT&a);AND aandba&&b
Yes boolT::operator&&(constT2&b)const;booloperator&&(constT&a,constT2&b);inclusive OR aorba||b
Yes boolT::operator||(constT2&b)const;booloperator||(constT&a,constT2&b);Notes
The keyword-like forms (and,or,not) and the symbol-like forms (&&,||,!) can be used interchangeably (see
).
All built-in operators return bool, and most
also return bool so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void).
Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands.
Explanation
The logic operator expressions have the form
!rhs(1) lhs&&rhs(2) lhs||rhs(3) 1) Logical NOT
2) Logical AND
3) Logical inclusive OR
If the operand is not bool, it is converted to bool using
: it is only well-formed if the declaration bool t(arg) is well-formed, for some invented temporary t.
The result is a bool prvalue.
For the built-in logical NOT operator, the result is true if the operand is false. Otherwise, the result is false.
For the built-in logical AND operator, the result is true if both operands are true. Otherwise, the result is false. This operator is
: if the first operand is false, the second operand is not evaluated.
For the built-in logical OR operator, the result is true if either the first or the second operand (or both) is true. This operator is short-circuiting: if the first operand is true, the second operand is not evaluated.
Note that
do not perform short-circuiting.
Results
atruefalse!afalsetrueandatruefalsebtruetruefalsefalsefalsefalseoratruefalsebtruetruetruefalsetruefalseIn
overload resolution against user-defined operators
, the following built-in function signatures participate in overload resolution:
booloperator!(bool)booloperator&&(bool,bool)booloperator||(bool,bool)Example
Run this code
#include<iostream>#include<sstream>#include<string>intmain(){intn=2;int*p=&n;// pointers are convertible to boolif(p&&*p==2// "*p" is safe to use after "p &&"||!p&&n!=2)// || has lower precedence than &&std::cout<<"true\n";// streams are also convertible to boolstd::stringstreamcin;cin<<"3...\n"<<"2...\n"<<"1...\n"<<"quit";std::cout<<"Enter 'quit' to quit.\n";for(std::stringline;std::cout<<"> "&&std::getline(cin,line)&&line!="quit";)std::cout<<line<<'\n';}Output:
true Enter 'quit' to quit. > 3... > 2... > 1... > Standard library
Because the short-circuiting properties of operator&& and operator|| do not apply to overloads, and because types with boolean semantics are uncommon, only two standard library classes overload these operators:
applies a unary arithmetic operator to each element of the valarray
(public member function of std::valarray<T>)applies binary operators to each element of two valarrays, or a valarray and a value
(function template)checks if an error has occurred (synonym of
)
(public member function of std::basic_ios<CharT,Traits>)
See also
Common operators
a=ba+=ba-=ba*=ba/=ba%=ba&=ba|=ba^=ba<<=ba>>=b++a--aa++a--+a-aa+ba-ba*ba/ba%b~aa&ba|ba^ba<<ba>>b!aa&&ba||ba==ba!=ba<ba>ba<=ba>=ba<=>ba[...]*a&aa->ba.ba->*ba.*bfunction calla(...)
commaa,b
conditionala?b:c
Special operators
converts one type to another related type
converts within inheritance hierarchies
adds or removes
-qualifiers
converts type to unrelated type
converts one type to another by a mix of static_cast, const_cast, and reinterpret_cast
creates objects with dynamic storage duration
destructs objects previously created by the new expression and releases obtained memory area
queries the size of a type
queries the size of a
(since C++11)
queries the type information of a type
checks if an expression can throw an exception (since C++11)
queries alignment requirements of a type (since C++11)
produces a reflection value from a grammatical construct (since C++26)