Topic: The difference between prefix ++ and postfix++
Author: jimad@microsoft.com (Jim Adcock)
Date: 10 Mar 93 18:15:40 GMT Raw View
In article <1993Mar8.210150.21239@njitgw.njit.edu> dic5340@hertz.njit.edu (David Charlap) writes:
|Unfortunately, the following code fragment:
|
| a = 2;
| cout << ++a * ++a;
|
|is still ambiguous. I've had two different C++ compilers return 12
|and 16 on this statement. And the statement
|
| a = 2;
| cout << ++a * a++;
|
|has returned 8, 9 and 12 from three different C++ compilers. It seems
|to me that these constructs shouldn't be ambiguous.
These constructs are not ambiguous. They are all unconstrained
programming errors. IE they are all expressions which legally
a programmer is not allowed to write, but which compilers are
not required to diagnose. See the ANSI C base document -- you
are not allowed to access a variable more than once in any expression
for the purpose of modifying its value. Nor are you allowed to
access a variable to modify its value and independently read the
variable to get its [which?] value.
There's a difference between implementation dependencies and
unconstrained errors. Programs using implementation dependencies
are legal, just not portable. Programs containing unconstrained errors
[or any other kind of error] are just plain and simple wrong.
Author: pcwu@csie.nctu.edu.tw ()
Date: Wed, 3 Mar 1993 02:05:24 GMT Raw View
In ANSI C, both the return type of prefix ++/-- and postfix ++/--
are not lvalues. But in C++, the prefix ++/-- are redefined as returning
lvalue. So you can write code like this:
int i;
++ ++ i;
but cannot write:
int i;
i ++ ++;
(Increment and decrement can only be applied on lvalue.)
What is the reason the language make prefix and postfix different?
Why do the committee make C++ incompatible with ANSI C without meaningful
improvements or advantages?
==============================
Pei-Chi Wu
Institute of C.S.I.E.
Nat'l Chiao-Tung Univ.
Hsinchu, Taiwan 30050
==============================
Author: koerbitz@Informatik.TU-Muenchen.DE ()
Date: Wed, 3 Mar 1993 12:07:45 GMT Raw View
Hi out there,
simple question from a beginner in object-oriented-etc. (:= oo-)
please send all available info, literature and faq-file
for follwing topics:
1. oo-systems-analysis and -design
2. oo-languages
3. oo-dbms
4. oo-analysis and design
5. oo-modeling techniques
If anyone has info please help me out.. * any help would be appreciated.
Thanks in advance
___________________________________________________________________________________
_/ _/_/ KETA - Thomas Koerbitz
_/ _/ Email koerbitz@informatik.tu-muenchen.de
_/ _/ _/_/_/_/ voice nat. 0 81 06 - 3 37 09
_/_/_/ _/ _/ int. +49 - 81 06 - 3 37 09
_/ _/ _/ _/ fax 0 81 06 - 3 43 81
_/ _/_/ _/ _/
__ __ __ __ __ __ __ _/_/ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ _
Author: pkt@lpi.liant.com (Scott Turner)
Date: Wed, 3 Mar 1993 21:44:41 GMT Raw View
In article <C3AJ50.KEp@csie.nctu.edu.tw>, pcwu@csie.nctu.edu.tw () writes:
> In ANSI C, both the return type of prefix ++/-- and postfix ++/--
> are not lvalues. But in C++, the prefix ++/-- are redefined as returning
> lvalue.
> ...
> What is the reason the language make prefix and postfix different?
The difference is based on the fact that prefix and postfix are different
in C. In C, prefix ++ increments the object and returns its incremented
value. In C++ an lvalue for the object itself is returned, at the time when
it holds the incremented value. This adds to the capabilities of prefix ++ without changing the meaning of C code.
In C, postfix ++ increments the object and returns its previous value.
If C++ were to return the object itself, the object would be evaluated
*after* it had been incremented. The desired previous value would not
be obtained.
> Why do the committee make C++ incompatible with ANSI C without meaningful
> improvements or advantages?
The change to prefix ++/-- is compatible with standard C because (as in C)
an lvalue is implicitly converted to a non-lvalue wherever appropriate.
The C++ standards committee does not introduce useless incompatibilities.
In fact, it is documenting and providing a justification for all
incompatibilities with C, even if they existed in C++ prior to the committee's
formation.
--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA (508) 872-8700
UUCP: uunet!lpi!pkt Internet: pkt@lpi.liant.com
Author: pcwu@csie.nctu.edu.tw ()
Date: Fri, 5 Mar 1993 05:57:32 GMT Raw View
Scott Turner (pkt@lpi.liant.com) wrote:
:
: The change to prefix ++/-- is compatible with standard C because (as in C)
: an lvalue is implicitly converted to a non-lvalue wherever appropriate.
C++ does extend the meaning of prefix ++/-- in returning lvalues.
It is questionable that such extension will be valuable. For C/C++ fundamental
types, prefix ++/-- can only be applied to arithmetic types or pointers. So
it is less useful allowing the following syntax:
int i;
++ ++i;
It is also confused to write down the following legal C++ but illegal C program
int i;
++i = 3;
For the typical C/C++ idioms:
char *p, *q;
while(*p++=*q++);
Postfix++ does a good job even if it does not return an lvalue. The
indirection of pointer (*) returns the needed lvalues.
For user-defined types (i.e., classes/objects), the user can overload
operator ++/-- to return reference types, and rewrite the rules for lvalue.
It is unrelated with the rules for fundamental types.
: The C++ standards committee does not introduce useless incompatibilities.
: In fact, it is documenting and providing a justification for all
: incompatibilities with C, even if they existed in C++ prior to the committee's
: formation.
In "The Annotated C++ Reference Manual", I cannot find any documentation
for the incompatility with ANSI C in prefix++/-- operators.
Regards.
==============================
Pei-Chi Wu
Institute of C.S.I.E.
Nat'l Chiao-Tung Univ.
Hsinchu, Taiwan 30050
e-mail: pcwu@csie.nctu.edu.tw
==============================
Author: steve@taumet.com (Steve Clamage)
Date: Sat, 6 Mar 1993 07:02:26 GMT Raw View
pcwu@csie.nctu.edu.tw (Pei-Chi Wu) writes:
> C++ does extend the meaning of prefix ++/-- in returning lvalues.
>It is questionable that such extension will be valuable. For C/C++ fundamental
>types, prefix ++/-- can only be applied to arithmetic types or pointers. So
>it is less useful allowing the following syntax:
> int i;
> ++ ++i;
>It is also confused to write down the following legal C++ but illegal C program
> int i;
> ++i = 3;
At the moment, C++ lacks the notion of "sequence points" found in Standard
C. If the notion is adopted, I don't think either of the expressions
++ ++i
++i = 3
would have a defined meaning. They each modify a variable twice between
sequence points. Perhaps this just strengthens your argument.
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Author: dic5340@hertz.njit.edu (David Charlap)
Date: 8 Mar 93 21:01:50 GMT Raw View
In article <1993Mar3.214441.17992@lpi.liant.com> pkt@lpi.liant.com (Scott Turner) writes:
>The difference is based on the fact that prefix and postfix are different
>in C. In C, prefix ++ increments the object and returns its incremented
>value. In C++ an lvalue for the object itself is returned, at the time when
>it holds the incremented value. This adds to the capabilities of
>prefix ++ without changing the meaning of C code.
>
>In C, postfix ++ increments the object and returns its previous value.
>If C++ were to return the object itself, the object would be evaluated
>*after* it had been incremented. The desired previous value would not
>be obtained.
Unfortunately, the following code fragment:
a = 2;
cout << ++a * ++a;
is still ambiguous. I've had two different C++ compilers return 12
and 16 on this statement. And the statement
a = 2;
cout << ++a * a++;
has returned 8, 9 and 12 from three different C++ compilers. It seems
to me that these constructs shouldn't be ambiguous. The first should
(logically) return 12, and the second should return 9, since (I think)
each side of the * would be evaluated, and the two return values
multiplied.
I know that the above expressions are undefined in C, but I would
think that they would have some definition under C++.
Interestingly enough,
a=2;
cout << (a+=1) * (a+=1);
always returns 12, as I would expect.
Of course, anyone who writes code like this is probably looking for
trouble anyway!
--
David Charlap dic5340@hertz.njit.edu
begin 666 signature.txt
B22!C86XG="!B96QI979E('EO=2!D96-O9&5D('1H:7,A"@
Author: pkt@lpi.liant.com (Scott Turner)
Date: Tue, 9 Mar 1993 14:13:20 GMT Raw View
In article <1993Mar8.210150.21239@njitgw.njit.edu>, dic5340@hertz.njit.edu (David Charlap) writes:
> Unfortunately, the following code fragment:
>
> a = 2;
> cout << ++a * ++a;
>
> is still ambiguous. I've had two different C++ compilers return 12
> and 16 on this statement.
> It seems to me that these constructs shouldn't be ambiguous.
> I know that the above expressions are undefined in C, but I would
> think that they would have some definition under C++.
C++ does nothing to make the above less undefined than it is in C.
> Interestingly enough,
>
> a=2;
> cout << (a+=1) * (a+=1);
>
> always returns 12, as I would expect.
Don't count on it. As far as the definition of C++ is concerned,
this is no different from the former code. Likewise for C.
--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA (508) 872-8700
UUCP: uunet!lpi!pkt Internet: pkt@lpi.liant.com