Logical operators - cppreference.com
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<metanoindex/>
Returns the result of a boolean operation.
| Operator name | Syntax | Overloadable | Prototype examples (for class T)
| |
|---|---|---|---|---|
| Inside class definition | Outside class definition | |||
| negation | not a
|
Yes | bool T::operator!() const;
|
bool operator!(const T &a);
|
| AND | a and b
|
Yes | bool T::operator&&(const T2 &b) const;
|
bool operator&&(const T &a, const T2 &b);
|
| inclusive OR | a or b
|
Yes | bool T::operator||(const T2 &b) const;
|
bool operator||(const T &a, const T2 &b);
|
| ||||
Spiegazione
The logical operators apply logic functions (NOT, AND, and inclusive OR) to boolean arguments (or types contextually-convertible to bool), with a boolean result. Unlike the bitwise logic operators, these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first.
Builtin operators
The following built-in function signatures participate in overload resolution:
<tbody> </tbody>
|
|
||
|
|
||
|
|
||
If the operand is not bool, it is converted to bool using contextual conversion to bool: it is only well-formed if the declaration bool t(arg) is well-formed, for some invented temporary t.
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. 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. If the firstoperand is true, the second operand is not evaluated.
Results
a
|
true
|
false
|
|---|---|---|
!a
|
false
|
true
|
and
|
a
| ||
|---|---|---|---|
true
|
false
| ||
b
|
true
|
true
|
false
|
false
|
false
|
false
| |
or
|
a
| ||
|---|---|---|---|
true
|
false
| ||
b
|
true
|
true
|
true
|
false
|
true
|
false
| |
Esempio
#include <iostream> #include <string> int main() { int n = 2; int* p = &n; // pointers are convertible to bool if( 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 bool std::cout << "Enter 'quit' to quit.\n"; for(std::string line; std::cout << "> " && std::getline(std::cin, line) && line != "quit"; ) ; }
Output:
true Enter 'quit' to quit. > test > quit
Libreria standard
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:
applica un operatore unario aritmetica per ogni elemento del valarray Original: applies a unary arithmetic operator to each element of the valarray The text has been machine-translated via Google Translate. (metodo pubblico) | |
vale operatori binari a ciascun elemento di due valarrays, o un valarray e un valore Original: applies binary operators to each element of two valarrays, or a valarray and a value The text has been machine-translated via Google Translate. (funzione di modello) | |
controlla se è verificato un errore (sinonimo di fail()) Original: The text has been machine-translated via Google Translate. (metodo pubblico) [modifica] | |
Vedi anche
| Common operators | ||||||
|---|---|---|---|---|---|---|
| assegnazione | incrementNJdecrement | aritmetica | logico | confronto | memberNJaccess | altra |
|
|
|
|
|
|
|
|
| Special operators | ||||||
|
static_cast
converte un tipo a un altro Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. dynamic_cast
converte classe virtuale di base per class Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. const_cast
converte il tipo di tipo compatibile con diversi cv qualifiers Original: const_cast converts type to compatible type with different cv qualifiers The text has been machine-translated via Google Translate. reinterpret_cast
converte tipo type Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. new
alloca memory Original: new allocates memory The text has been machine-translated via Google Translate. delete
dealloca memory Original: delete deallocates memory The text has been machine-translated via Google Translate. sizeof
interroga la dimensione di un type Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. sizeof...
interroga le dimensioni di un parametro confezione (dal C++11) Original: sizeof... queries the size of a parametro confezione (dal C++11) The text has been machine-translated via Google Translate. typeid
interroga le informazioni sul tipo di una type Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. noexcept
controlla se un'espressione può lanciare una (dal C++11) Original: noexcept checks if an expression can throw an exception (dal C++11) The text has been machine-translated via Google Translate. alignof
query requisiti di allineamento di un (dal C++11) tipo Original: alignof queries alignment requirements of a type (dal C++11) The text has been machine-translated via Google Translate. | ||||||