By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Prefix/postfix ops -- concept question

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* Sergey:

Why C++ make two ops for prefix and postfix ++ operator?


The postfix version is for writing side-effect based code, a sure way to
invoke Undefined Behavior (but presumably a nice feature in C as of 1972,
where the programmer had to do much that the compiler does nowadays).

See <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_11.html>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a

"Sergey" <se********@narod.ru> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Jul 23 '05 #3

P: n/a
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.

Jul 23 '05 #4

P: n/a
Sergey wrote:
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
Jul 23 '05 #5

P: n/a
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?

Jul 23 '05 #6

P: n/a
Sergey wrote:
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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.