C++(www.cppentry.com) provides a rich set of operators and defines what these operators do when applied to operands of built-in type. It also allows us to define meanings for the operators when applied to class types. This facility, known as operator overloading, is used by the library to define the operators that apply to the library types.
C++(www.cppentry.com) 语言里的算术表达式和其他一般编程(www.cppentry.com)语言没有多大差别,值得注意的是C++(www.cppentry.com) 独有的一些表达式。另外,表达式的求值顺序(order of eva luation)也值得注意,应该避免写出依赖于特定求值顺序的代码。
In this chapter our focus is on the operators as defined in the language and applied to operands of built-in type. We will also look at some of the operators defined by the library. Chapter 14 shows how we can define our own overloaded operators.
An expression is composed of one or more operands that are combined by operators. The simplest form of an expression consists of a single literal constant or variable. More complicated expressions are formed from an operator and one or more operands.
Every expression yields a result. In the case of an expression with no operator, the result is the operand itself, e.g., a literal constant or a variable. When an object is used in a context that requires a value, then the object is eva luated by fetching the object’s value. For example, assuming ival is an int object,
- if (ival)
-
we could use ival as an expression in the condition of an if. The condition succeeds if the value of ival is not zero and fails otherwise.
The result of expressions that involve operators is determined by applying each operator to its operand(s). Except when noted otherwise, the result of an expression is an rvalue (Section 2.3.1, p. 45). We can read the result but cannot assign to
it.
The meaning of an operator—what operation is performed and the type of the result—depends on the types of its operands.
Until one knows the type of the operand(s), it is not possible to know what a particular expression means. The expression
- i + j
might mean integer addition, concatenation of strings, floating-point addition, or something else entirely. How the expression is eva luated depends on the types of i and j.
There are both unary operators and binary operators. Unary operators, such as address-of (&) and dereference (*), act on one operand. Binary operators, such as addition (+) and subtraction (-), act on two operands. There is also one ternary operator
that takes three operands. We’ll look at this operator in Section 5.7 (p. 165).
Some symbols, such as *, are used to represent both a unary and a binary operator. The * symbol is used as the (unary) dereference operator and as the (binary) multiplication operator. The uses of the symbol are independent; it can be helpful
to think of them as two different symbols. The context in which an operator symbol is used always determines whether the symbol represents a unary or binary operator.
Operators impose requirements on the type(s) of their operand(s). The language defines the type requirements for the operators when applied to built-in or compound types. For example, the dereference operator, when applied to an object of built-in type, requires that its operand be a pointer type. Attempting to dereference an object of any other built-in or compound type is an error.
The binary operators, when applied to operands of built-in or compound type, usually require that the operands be the same type, or types that can be converted to a common type. We’ll look at conversions in Section 5.12 (p. 178). Although the rules can be complex, for the most part conversions happen in expected ways.
表达式由操作数(operands)和连接操作数的操作符(operators)组成;表达式可以没有操作符,但是不能没有操作数。
一个表达式除了有运算结果(result,即表达式的值),还可能有副作用(sideeffect)。我们在程序里写一个表达式,有可能是为了要它的结果,也有可能是为了要它的副作用,或者二者皆有。
C++(www.cppentry.com) 里一个表达式的意义(类型、值、副作用)往往要由它的操作数的类型来决定,若脱离上下文单独地去看一个表达式,我们较难明白它的用处。
Understanding expressions with multiple operators requires understanding operator precedence, associativity, and the order of eva luation of the operands. For example, the expression
- 5 + 10 * 20/2;
uses addition, multiplication, and division. The result of this expression depends on how the operands are grouped to the operators. For example, the operands to the * operator could be 10 and 20, or 10 and 20/2, or 15 and 20 or 15 and 20/2. Associativity and precedence rules specify the grouping of operators and their operands. In C++(www.cppentry.com) this expression eva luates to 105, which is the result of multiplying 10 and 20, dividing that result by 2, and then adding 5.
Knowing how operands and operators are grouped is not always sufficient to determine the result. It may also be necessary to know in what order the operands to each operator are eva luated. Each operator controls what assumptions, if any, can be made as to the order in which the operands will be eva luated—that is, whether we can assume that the left-hand operand is always eva luated before the right or not. Most operators do not guarantee a particular order of eva luation. We will cover these topics in Section 5.10 (p. 168).
C++(www.cppentry.com) 既有一元操作符,也有二元操作符,还有一个特殊的三元操作符。
如果一个表达式里有不止一个操作符,那么我们要先弄明白操作符的优先级、结合性,以及操作数的求值顺序。