473,419 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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 2225
* 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********@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
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
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
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 // //...
98
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
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...
30
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”....
8
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
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...
3
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...
2
by: puzzlecracker | last post by:
How can we implement operator ++ as postfix in prefix? thanks
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.