FAQ [13.9] intrinsic behavior of operator++(), operator++(int)

Discussion in 'C++' started by gob00st@googlemail.com, Feb 20, 2009.

  1. Guest

    FAQ [13.9]
    <Quote>
    9.If you define x++ and ++x, maintain the usual identities. For
    example, x++ and ++x should have should have the same observable
    effect on x, and should differ only in what they return. ++x should
    return x by reference; x++ should either return a copy (by value) of
    the original state of x or should have a void return-type. You're
    usually better off returning a copy of the original state of x by
    value, especially if your class will be used in generic algorithms.
    </Quote>

    I understand this suggesting we should try to keep user defined post/
    pre incremental operator to mimic the behaviour of intrinsic type, but
    any idea
    why pre-incremental returns reference and post-incremental returns a
    copy .

    Regards,
    Gob00st
    , Feb 20, 2009
    #1
    1. Advertising

  2. Daniel T. Guest

    On Feb 20, 11:43 am, wrote:
    > FAQ [13.9]
    > <Quote>
    > 9.If you define x++ and ++x, maintain the usual identities. For
    > example, x++ and ++x should have should have the same observable
    > effect on x, and should differ only in what they return. ++x should
    > return x by reference; x++ should either return a copy (by value) of
    > the original state of x or should have a void return-type. You're
    > usually better off returning a copy of the original state of x by
    > value, especially if your class will be used in generic algorithms.
    > </Quote>
    >
    > I understand this suggesting we should try to keep user defined post/
    > pre incremental operator to mimic the behaviour of intrinsic type, but
    > any idea
    > why pre-incremental returns reference and post-incremental returns a
    > copy .


    With pre-increment, x equals the value returned. With post-increment,
    x doesn't equal the value returned. So for the former, you can return
    a reference, but for the latter you have to return a copy.
    Daniel T., Feb 20, 2009
    #2
    1. Advertising

  3. Guest

    On Feb 20, 5:35 pm, "Daniel T." <> wrote:
    > On Feb 20, 11:43 am, wrote:
    >
    >
    >
    > > FAQ [13.9]
    > > <Quote>
    > > 9.If you define x++ and ++x, maintain the usual identities. For
    > > example, x++ and ++x should have should have the same observable
    > > effect on x, and should differ only in what they return. ++x should
    > > return x by reference; x++ should either return a copy (by value) of
    > > the original state of x or should have a void return-type. You're
    > > usually better off returning a copy of the original state of x by
    > > value, especially if your class will be used in generic algorithms.
    > > </Quote>

    >
    > > I understand this suggesting we should try to keep user defined post/
    > > pre incremental operator to mimic the behaviour of intrinsic type, but
    > > any idea
    > > why pre-incremental returns reference and post-incremental returns a
    > > copy .

    >
    > With pre-increment, x equals the value returned. With post-increment,
    > x doesn't equal the value returned. So for the former, you can return
    > a reference, but for the latter you have to return a copy.

    Thanks for the reply.
    So does that mean for pre-increment , return reference is not
    mandatory but recommenced only for efficiency?
    , Feb 20, 2009
    #3
  4. writes:

    > On Feb 20, 5:35 pm, "Daniel T." <> wrote:
    >> On Feb 20, 11:43 am, wrote:
    >>
    >>
    >>
    >> > FAQ [13.9]
    >> > <Quote>
    >> > 9.If you define x++ and ++x, maintain the usual identities. For
    >> > example, x++ and ++x should have should have the same observable
    >> > effect on x, and should differ only in what they return. ++x should
    >> > return x by reference; x++ should either return a copy (by value) of
    >> > the original state of x or should have a void return-type. You're
    >> > usually better off returning a copy of the original state of x by
    >> > value, especially if your class will be used in generic algorithms.
    >> > </Quote>

    >>
    >> > I understand this suggesting we should try to keep user defined post/
    >> > pre incremental operator to mimic the behaviour of intrinsic type, but
    >> > any idea
    >> > why pre-incremental returns reference and post-incremental returns a
    >> > copy .

    >>
    >> With pre-increment, x equals the value returned. With post-increment,
    >> x doesn't equal the value returned. So for the former, you can return
    >> a reference, but for the latter you have to return a copy.

    > Thanks for the reply.
    > So does that mean for pre-increment , return reference is not
    > mandatory but recommenced only for efficiency?


    I think a lot of the motivation behind the suggestion is to mimic what
    the built-in operators do. x++ is just a value but ++x is defined as
    x += 1 whose result is an lvalue (something that can be assigned to).
    Returning a reference is the way to make the result of a user-defined
    operator acceptable on the left hand side of an assignment.

    --
    Ben.
    Ben Bacarisse, Feb 20, 2009
    #4
  5. Guest

    On Feb 20, 7:01 pm, Victor Bazarov <> wrote:
    > wrote:
    > > On Feb 20, 5:35 pm, "Daniel T." <> wrote:
    > >> On Feb 20, 11:43 am, wrote:

    >
    > >>> FAQ [13.9]
    > >>> <Quote>
    > >>> 9.If you define x++ and ++x, maintain the usual identities. For
    > >>> example, x++ and ++x should have should have the same observable
    > >>> effect on x, and should differ only in what they return. ++x should
    > >>> return x by reference; x++ should either return a copy (by value) of
    > >>> the original state of x or should have a void return-type. You're
    > >>> usually better off returning a copy of the original state of x by
    > >>> value, especially if your class will be used in generic algorithms.
    > >>> </Quote>
    > >>> I understand this suggesting we should try to keep user defined post/
    > >>> pre incremental operator to mimic the behaviour of intrinsic type, but
    > >>> any idea
    > >>> why pre-incremental returns reference and post-incremental returns a
    > >>> copy .
    > >> With pre-increment, x equals the value returned. With post-increment,
    > >> x doesn't equal the value returned. So for the former, you can return
    > >> a reference, but for the latter you have to return a copy.

    > > Thanks for the reply.
    > > So does that mean for pre-increment , return reference is not
    > > mandatory but recommenced only for efficiency?

    >
    > Not for the efficiency, but to *mimic the behaviour of the intrinsic
    > types*.  You wrote it yourself...  The increment operators for built-in
    > types return an lvalue if pre-, and an rvalue if post-.  To mimic that
    > you usually return a reference if lvalue is needed, and an object otherwise.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >
    > - Show quoted text -


    I see. Thanks for the reply.
    , Feb 20, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    449
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    942
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    428
    James Kanze
    Apr 17, 2007
Loading...

Share This Page