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

G

gob00st

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
 
D

Daniel T.

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.
 
G

gob00st

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?
 
B

Ben Bacarisse

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.
 
G

gob00st

On Feb 20, 11:43 am, (e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top