Logical operators — cppreference.com
De cppreference.com
<metanoindex/>
Retourne le résultat d'une opération booléenne .
Original:
Returns the result of a boolean operation.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
| 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);
|
| ||||
Explication
Les opérateurs logiques s'appliquent fonctions logiques (NOT, AND, OR et inclusive) aux arguments booléens (ou types contexte convertibles à bool), avec un résultat booléen. Contrairement à la opérateurs logiques bit à bit, ces opérateurs (dans leur formulaire intégré) n'évaluent pas le second opérande, si le résultat est connu après une évaluation de la première .
Original:
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 opérateurs logiques bit à bit, these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Opérateurs intégrés
Les signatures suivantes fonction intégrée de participer à la résolution de surcharge:
Original:
The following built-in function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
<tbody> </tbody>
|
|
||
|
|
||
|
|
||
Si l'opérande n'est pas bool, il est converti en utilisant bool conversion contextuelle bool: ce n'est que bien formée si la déclaration bool t(arg) est bien formée, pour certains t inventé temporaire .
Original:
If the operand is not bool, it is converted to bool using conversion contextuelle bool: it is only well-formed if the declaration bool t(arg) is well-formed, for some invented temporary t.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Pour le haut-opérateur logique NOT, le résultat est true si l'opérande est false. Dans le cas contraire, le résultat est false .
Original:
For the built-in logical NOT operator, the result is true if the operand is false. Otherwise, the result is false.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Pour le haut-opérateur AND logique, le résultat est true si les deux opérandes sont true. Dans le cas contraire, le résultat est false. Si le premier opérande est false, le second opérande n'est pas évalué .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Pour le haut-opérateur logique OU, le résultat est true si le premier ou le deuxième opérande (ou les deux) est true. Si le firstoperand est true, le second opérande n'est pas évalué .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Résultats
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
| |
Exemple
#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"; ) ; }
Résultat :
true Enter 'quit' to quit. > test > quit
Bibliothèque standard
Comme les propriétés de court-circuit de operator&& et operator|| ne s'appliquent pas aux surcharges, et parce que les types booléens avec la sémantique sont rares, seulement deux classes de la bibliothèque standard surcharger ces opérateurs:
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
applique un opérateur arithmétique unaire pour chaque élément de la valarray Original: applies a unary arithmetic operator to each element of the valarray The text has been machine-translated via Google Translate. (fonction membre publique de std::valarray)
| |
des opérateurs binaires s'applique à chaque élément de valarrays deux, ou un valarray et une valeur 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. (fonction générique) | |
vérifie si une erreur s'est produite (synonyme de fail()) Original: The text has been machine-translated via Google Translate. (fonction membre publique de std::basic_ios) [edit]
| |
Voir aussi
| Opérateurs ordinaires | ||||||
|---|---|---|---|---|---|---|
| affectation | incrémentation décrémentation |
arithmétique | logique | comparaison | accès aux membre | autre |
|
|
|
|
|
|
|
|
| Opérateurs spéciaux | ||||||
|
static_cast convertit un type dans un autre type compatible dynamic_cast convertit une classe de base virtuelle dans une classe dérivée const_cast convertit un type dans un type compatible avec des cv-qualifiers différents reinterpret_cast convertit un type dans un type incompatibles new allocation de la mémoire delete libère de la mémoire sizeof récupère la taille d'un type sizeof... récupère la taille d'un paquet de paramètres (depuis C++11) typeid récupère les informations de type d'un type noexcept vérifie si une expression peut lancer une exception (depuis C++11) alignof récupère les conditions d'alignement d'un type (depuis C++11) | ||||||