Topic: C++ unary prefix and postfix ++/--


Author: aldrich@sequent.com (Jonathan Aldrich)
Date: Mon, 1 Aug 94 22:07:49 GMT
Raw View
In article <PRANGE.94Jul27194718@slbwa7.bln.sel.alcatel.de>,
Martin Prange <prange@slbwa7.bln.sel.alcatel.de> wrote:
>Distribution: world
>
>
>>
>> I have questions about prefix and postfix ++/--.
>>
>> 1. Why ++i and --i are lvalue, but i++ and i-- are not?  My Sun C++ Compiler
>>    3.0 gives me warnings when I pass i++ or i-- to a routine (e.g. twice(int&)
>>    in my code) which takes a reference argument.  Those two calls with i++ and
>>    i-- even do not give me the answer that I expected.
>>
>>    In addition, are the definitions showing in the second paragraph true in
>>    C++?
>>
>
>Think about it!
>
>prefix operator ++ or -- for Class Bla ( returning lvalues):
> They increment the Object (*this) - whatever that`l mean -
> and
>  return (*this)
> as Bla& !
>
>This is possible in-situ !
>
>But HOW will you archieve this with postfix operators ???
>
>    Hope this helps.
>
>     Ciao
>      Martin Prange

You could do it by having the compiler call postfix operator ++ AFTER the
procedure was run.  In other words:

void foo(int &i);

foo(number++);

would be equivalent to

foo(number); /* passes number as a reference; number may be changed */
number++; /* the new value of number is incremented */

which is the logical meaning of a postfix operator.  Say number was 3 before,
and foo multiplies it by 2.  The intent of the statement foo(number++) is
clearly to have foo run its operations (producing 6) and then incrementing
number (to 7).  What is wrong with this?  I know that's not how the ARM
defines it, but it could be discussed (and changed) by the standards committee.

This change would eliminate having to add an extra int argument to indicate
a postfix increment--the compiler would simply call the increment before or
after the procedure call as indicated by prefix or postfix syntax.  It
strikes me as a much simpler and more intuitive solution than the current
implementation.

Do you like it? hate it? think it puts restrictions on programming? think it
will break tons of code?





Author: jason@cygnus.com (Jason Merrill)
Date: Tue, 2 Aug 1994 09:39:37 GMT
Raw View
>>>>> Jonathan Aldrich <aldrich@sequent.com> writes:

> You could do it by having the compiler call postfix operator ++ AFTER the
> procedure was run.

As I said in another message in this thread, that can't happen because
there is a sequence point between the execution of the operator and the
function call.

Jason




Author: kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425)
Date: 02 Aug 1994 14:35:43 GMT
Raw View
In article <1994Aug1.220749.176@sequent.com> aldrich@sequent.com
(Jonathan Aldrich) writes:

|> In article <PRANGE.94Jul27194718@slbwa7.bln.sel.alcatel.de>,
|> Martin Prange <prange@slbwa7.bln.sel.alcatel.de> wrote:

|> >prefix operator ++ or -- for Class Bla ( returning lvalues):
|> > They increment the Object (*this) - whatever that`l mean -
|> > and
|> >  return (*this)
|> > as Bla& !

|> >This is possible in-situ !

|> >But HOW will you archieve this with postfix operators ???

|> You could do it by having the compiler call postfix operator ++ AFTER the
|> procedure was run.  In other words:

|> void foo(int &i);

|> foo(number++);

|> would be equivalent to

|> foo(number); /* passes number as a reference; number may be changed */
|> number++; /* the new value of number is incremented */

|> which is the logical meaning of a postfix operator.  Say number was 3 before,
|> and foo multiplies it by 2.  The intent of the statement foo(number++) is
|> clearly to have foo run its operations (producing 6) and then incrementing
|> number (to 7).  What is wrong with this?  I know that's not how the ARM
|> defines it, but it could be discussed (and changed) by the standards committee.

This would involve changing the semantics of a currently defined
operation.  In fact, it would involve changing the semantics of the C
subset of C++.  And silently.

I can think of a lot of people who would be very unhappy if this
happened.

|> This change would eliminate having to add an extra int argument to indicate
|> a postfix increment--the compiler would simply call the increment before or
|> after the procedure call as indicated by prefix or postfix syntax.  It
|> strikes me as a much simpler and more intuitive solution than the current
|> implementation.

|> Do you like it? hate it? think it puts restrictions on programming? think it
|> will break tons of code?

I think it would break a significant amount of code, although I am not
sure how much.  I also think that it would be very difficult to
specify exactly when the incrementation takes place.
--
James Kanze                                  email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue des Francs Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung




Author: prange@slbwa7.bln.sel.alcatel.de (Martin Prange)
Date: Wed, 27 Jul 1994 17:47:18 GMT
Raw View
Distribution: world


>
> I have questions about prefix and postfix ++/--.
>
> 1. Why ++i and --i are lvalue, but i++ and i-- are not?  My Sun C++ Compiler
>    3.0 gives me warnings when I pass i++ or i-- to a routine (e.g. twice(int&)
>    in my code) which takes a reference argument.  Those two calls with i++ and
>    i-- even do not give me the answer that I expected.
>
>    In addition, are the definitions showing in the second paragraph true in
>    C++?
>

Think about it!

prefix operator ++ or -- for Class Bla ( returning lvalues):
 They increment the Object (*this) - whatever that`l mean -
 and
  return (*this)
 as Bla& !

This is possible in-situ !

But HOW will you archieve this with postfix operators ???

    Hope this helps.

     Ciao
      Martin Prange
--
-- % ---------------------------------------------------------------%_|_|_|+
  /  prange@sel.bln.alcatel.de       Alcatel-SEL Berlin             |_|_|
 \\ If you believe, the opinions expressed here are mine ---       _|_|
  \\__ you got it ! ______________________________________________|_|




Author: jason@cygnus.com (Jason Merrill)
Date: Thu, 28 Jul 1994 01:08:36 GMT
Raw View
>>>>> Andrew Chen <achen@elst7.lsil.com> writes:

> 1. Why ++i and --i are lvalue, but i++ and i-- are not?  My Sun C++
>    Compiler 3.0 gives me warnings when I pass i++ or i-- to a routine
>    (e.g. twice(int&) in my code) which takes a reference argument.  Those
>    two calls with i++ and i-- even do not give me the answer that I
>    expected.

Because the semantics of i++ are 'note the value of i, then increment it'.
Since there is a sequence point between the evaluation of i++ and the
function call, the increment must take place before the function can be
called; but then there's no variable with the old value to pass by
reference.

So you build a temporary to hold the old value.  Which is an anachronism.

Jason




Author: achen@elst7.lsil.com (Andrew Chen)
Date: 26 Jul 1994 22:51:01 GMT
Raw View
I have questions about prefix and postfix ++/--.

1. Why ++i and --i are lvalue, but i++ and i-- are not?  My Sun C++ Compiler
   3.0 gives me warnings when I pass i++ or i-- to a routine (e.g. twice(int&)
   in my code) which takes a reference argument.  Those two calls with i++ and
   i-- even do not give me the answer that I expected.

   In addition, are the definitions showing in the second paragraph true in
   C++?

-----------------------------------------------------------------------------
void
twice(int& i)
{
    i *= 2;
}

int
main()
{
    int i;

    // Warning (Anachronism):
    // Temporary created for argument i in call to twice(int&).
    i = 6; twice(i++); cout << i << endl;  // output is 7, not 13 as expected

    // Warning (Anachronism):
    // Temporary created for argument i in call to twice(int&).
    i = 6; twice(i--); cout << i << endl;  // output is 5, not 11 as expected

    // no problem with these two.
    i = 6; twice(++i); cout << i << endl;  // output is 14 as expected
    i = 6; twice(--i); cout << i << endl;  // output is 10 as expected

    return 0;
}
-----------------------------------------------------------------------------

2. So, C++ prefix and postfix ++/-- are implicitly defined as:
-----------------------------------------------------------------------------
    // prefix ++
    int&
    operator ++(int& i)
    {
        i += 1;
        return i;
    }

    // prefix --
    int&
    operator --(int& i)
    {
        i -= 1;
        return i;
    }

    // postfix ++
    int
    operator ++(int& i, int)
    {
        i += 1;
        return i;  // value copied and returned
    }

    // postfix --
    int
    operator --(int& i, int)
    {
        i += 1;
        return i;  // value copied and returned
    }
-----------------------------------------------------------------------------

Andrew Chen
+--------------------------------------------------------+-----+---+
|  ANDREW CHEN                                        LSI|LOGIC|   |
|  LSI Logic Corp.                                       |     |   |
|  1501 McCarthy Blvd.      e-mail: achen@lsil.com       +-----+   |
|  M/S E-199                phone:  408-433-8749                   |
|  Milpitas, CA 95035       fax:    408-433-6802                   |
+------------------------------------------------------------------+