ctr++ or ++ctr

Ö

Öö Tiib

I prefer to write "i++" rather than "++i", but that's a matter of taste.
Arguably it is best to write "++i" rather than getting into the had
habit of "i++".

Lot of people seem to have same taste as you.
As for code differences, a compiler doesn't need much optimising to
generate identical code for "i++" and "++i" when "i" is an integer type
and you are not using the value of the expression (in which case, code
will be different of course). It is not "optimising heavily" -
compilers should do this at even their most minimal optimisation levels,
which would certainly be enabled in normal usage.

I did not mean optimizing incrementing scalars like pointers or integers.
Such things have been parts of some other instruction (in style
"dereference and increment") in older processors. Modern processor
architectures have simpler operations and processors optimize (combine
and reorder) those.
Once you are talking about something other than an integer or pointer,
such as a class with overloaded operators, then there can be big
differences.

That was what I meant: there can be big differences in theory and it
can be measured from maliciously made code but there are no noticeable
differences in practice and when measuring algorithms of real code base.

The reasons are that the overloaded 'operator++'s are usually for
lightweight classes (numeric, counter, time, iterator) that are often
templates and the operators are often inline. On case of iterators
even using code is often inline (in <algorithm> etc.) That means
compilers have lot of opportunities to optimize there on common
case.
 
Ö

Öö Tiib

i++ is a convention in the sense that:
- it's a convention in C; look at any for(...) loop

It is even good when other similar languages (like C, Java, lua, C# or
Objective-?) have different conventions. That helps to recognize what is
what faster.
- there are people who prefer to inherit into C++ too (including some
experienced c.l.c++ regulars[1])
....

[1] Based on a discussion here a few years ago, and I don't recall any
names. I argued for ++i, but was convinced my arguments didn't hold.

FAQ supports your opinion. That means they did hold. ;)
 
S

SG

This is not a requirement. Nowadays compilers are also doing cross-
translation unit optimizations. Also, the compiler could generate two
versions of the function with different linker names for different usage
scenarios, and linker could throw away any unused versions. Don't know if
any compiler bothers to do such things here.


Copy constructors are specifically allowed to be optimized away (together
with the corresponding destructor call of course) even if they have side
effects.

Not in this case. In this case the optimizer would have to prove equivalence w.r.t. possible side effects under the as-if rule.

I don't know about you guys, but I see no point in counting on the compileroptimization abilities when I could just as well write ++x instead of x++.I use preincrement on pretty much everything for consistency if I don't need the old value. Yes, I know that it won't make a difference with ints. Sowhat?

Cheers,
SG
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top