Logical operators
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
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); |
|
目录 |
[编辑] 解释
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:
bool operator!(bool) |
||
bool operator&&(bool, bool) |
||
bool operator||(bool, bool) |
||
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 |
[编辑] 示例
#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"; ) ; }
输出:
true Enter 'quit' to quit. > test > quit
[编辑] 标准库
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:
适用于一元算术运算符的valarray的每个元素 原文: applies a unary arithmetic operator to each element of the valarray (公共成员函数of std::valarray )
| |
二元运算符的每个元素的两个valarrays,或valarray的和值 原文: applies binary operators to each element of two valarrays, or a valarray and a value (函数模板) | |
(公共成员函数of std::basic_ios )
|
[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||