473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prefix/postfix ops -- concept question

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
6 2236
* 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

"Sergey" <se********@nar od.ru> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3623
by: Christian Christmann | last post by:
Hi, is there a difference in terms of efficiency when using a prefix "++x;" instead of a postfix "x++;"? Thanks Chris
8
6241
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // // >g++ test.cpp // test.cpp: In function `int main()':
98
14324
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
2
2248
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program with output.
30
5435
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”. Likewise, they write “(if test this that)” to mean “if (test) {this} else {that}”. LISP codes are all of the form “(a b c ...)”, where the
8
7823
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
2
2492
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is used as a prefix, the operation is performed before the function call. If the operator is used as a postfix, the operation is performed after the...
3
2026
by: subramanian100in | last post by:
Consider the code fragment: vector<intcontainer; container.insert(container.begin(), 10); int& ref = *--container.end(); From this, it looks like we can apply prefix decrement operator to container.end() - ie 'prefix --' can be applied to the iterator type
2
2440
by: puzzlecracker | last post by:
How can we implement operator ++ as postfix in prefix? thanks
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.