473,289 Members | 1,953 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,289 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 2220
* 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
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.