home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Answers on a postcard...
- Message-ID: <1992Sep11.174716.3699@taumet.com>
- Organization: TauMetric Corporation
- References: <92254.104002KKEYTE@ESOC.BITNET> <1992Sep10.174914.838@taumet.com> <92255.112151KKEYTE@ESOC.BITNET>
- Date: Fri, 11 Sep 1992 17:47:16 GMT
- Lines: 50
-
- Karl Keyte <KKEYTE@ESOC.BITNET> writes:
-
- >>In the expression in parens, it is unspecified whether "x=1" or
- >>"x+1" is evaluated first. They are arguments to the "operator,()"
- >>function, not the left and right side of a comma-expression. If
- >>"x+1" is evaluated first, which is legal, it will use an
- >>uninitialized "x.value".
-
- >The implication of what you say is that it is not possible to overload
- >operators maintaining the correct functionality of the operator. This
- >indeed means that precedence in overloaded operators cannot be maintained
- >if all operands are simply passed as arguments to the function implementing
- >the operator functionality and evaluated in a language independent order.
- >Imagine commonly used "chain" operators such as && and ║║.
-
- First, precedence and order of evaluation are different concepts.
- In the expression
- a + b * c
- (where a, b, and c may themselves be parenthesized expressions)
- fixed precedence means the expression is always equivalent to
- a + (b * c)
- independent of operator overloading. Overloading does not change
- precedence. You still do not know which of 'a', 'b', and 'c' is
- evaluated first and which is evaluated last. You also do not
- know whether 'a' is evaluated before or after the '*' operation
- is performed. Within the limits of precedence, the order of
- evaluation is unspecified. Precedence does require that '*'
- be performed before '+', since '+' requires the result of '*'.
-
- Second, what is the "correct" functionality of an overloaded operator?
- The language imposes no restrictions on the semantics of overloaded
- operators (except for ->). You could define "&&" to do literally
- anything; it need not be any sort of logical "and".
-
- The non-overloaded && and || enforce an order of evaluation because
- their value can be potentially be known without evaluating the
- second operand. The conditional evaluation makes for compact
- expressions. An overloaded operator && or || cannot be assumed
- to have such behavior, and both operands must always be evaluated.
-
- The only other overloadable operator which imposes order of evaluation
- is the comma. Originally it was not overloadable, since there seemed
- to be no good reason to allow it. It looks like we have found a
- good reason to disallow it, namely that the implied order of
- evaluation no longer holds. (Too bad it is so hard to stuff the
- genie back into the bottle.)
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-