Prefix/postfix ops -- concept question

S

Sergey

Hello!

Could anybody be kind enough to explain this concept?

Why C++ make two ops for prefix and postfix ++ operator?
As I guess, it is possible to implement both cases via sole
prefix increment operation. Correct me if I'm wrong,

Let's see trival example:

Complex a=3,b;

b=a++-2;

Compiler could rewrite this as:

b=a;
++a;
b-=2;

So. why make extraneous overhead with temporary var for postfix
increment and 2 distinct ops?

Thank you for patience, Sergey.
 
A

Allan Bruce

Sergey said:
Hello!

Could anybody be kind enough to explain this concept?

Why C++ make two ops for prefix and postfix ++ operator?
As I guess, it is possible to implement both cases via sole
prefix increment operation. Correct me if I'm wrong,

Let's see trival example:

Complex a=3,b;

b=a++-2;

Compiler could rewrite this as:

b=a;
++a;
b-=2;

So. why make extraneous overhead with temporary var for postfix
increment and 2 distinct ops?

Thank you for patience, Sergey.

Or the compiler could rewrite it as:
b=a-2;
a++;

The point is the compiler can do whatever it wants as long as the result the
same. This is not topical in this newsgroup since it doesnt matter to most
users of C++. The point of having the postfix operator is useful when
trying to use a single line statement, e.g.

if (someCondition)
a = b[x++];

Here you cannot use ++x without introducing {} which as far as style goes
may be desired or undesired. Postfix ++ also has a different meaning with
classes which might be desirable under some circumstances.

Allan
 
S

Sergey

Thank you for answer, Allan.

I mean why compiler make us care about operator++() and
operator++(int),
when it could just call our prefix ++ operator.

Let's see:

if (someCondition)
a = b[x++];

Could be transparently (by compiler, not by user) rewriten as:

if (someCondition)
{
a=b[x];
x++;
}

So why it make us define two different ops introducing
extraneous temporary value?

Sergey.
 
V

Victor Bazarov

Sergey said:
Thank you for answer, Allan.

I mean why compiler make us care about operator++() and
operator++(int),
when it could just call our prefix ++ operator.

Let's see:

if (someCondition)
a = b[x++];

Could be transparently (by compiler, not by user) rewriten as:

if (someCondition)
{
a=b[x];
x++;
}

So why it make us define two different ops introducing
extraneous temporary value?

If you write it in an expression, the language rules regarding sequence
points allow the compiler to better optimize the code. Having an extra
semicolon imposes unnecessary limitations on the optimization process.

Using your words, why it make us write two separate statements when one
is sufficient?

V
 
S

Sergey

Hmmm ... interesting concept, but creating new class instance may cause
serious
perfomance degrade. So it seems more safely to make new statement
instead of new instance. Why the worst case - temporary variable (new
instance) was choosed?
 
V

Victor Bazarov

Sergey said:
Hmmm ... interesting concept, but creating new class instance may cause
serious
perfomance degrade. So it seems more safely to make new statement
instead of new instance. Why the worst case - temporary variable (new
instance) was choosed?

I am not sure how to answer your question. Perhaps Kernighan or Ritchie
answer it somehow in their writings on C language development. The
difference between prefix and postfix increment and decrement operators
exists in C++ from C, and in C probably from some other earlier language
(although I do not know if that's true or not).

The necessity to create (and return) a temporary object exists because
there seems to be no other way to both increment the object and return its
_previous value_ with post-increment, if you decide to follow the
semantics of post-increment defined for built-in types.

Both versions of increment or decrement operator are useful, if you think
about it. They both allow the compiler to generate optimized code. What
else is there, I don't know. But IMO that should be enough of a reason to
keep them in C++.

V
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top