pete said:
Malcolm wrote: [...]
However in C++, because of a quirk of the language,
the form ++C is faster than the form C++
for overloaded types with complex logic.
You're off topic and I don't believe you.
Here the ++C form should be used.
I disagree.
Please note the '[OT]' tag in the subject. I'm discussing this here
partly to contrast C++ vs. C.
In C++, the "++" operator can be overloaded so it invokes a
user-defined function; the meaning of "increment" is determined by the
function itself, and can involve arbitrarily complex computations.
For x++, the result is the value of x before it's "incremented". In
some cases, that means that previous value of x has to be saved
somewhere, then x is "incremented", then the saved value becomes the
value of the expression. For a user-defined type, this can introduce
significant overhead, and it can be difficult for the compiler to
optimize it away. For ++x, the value of x is "incremented" and the
result is the new value of x; the old value doesn't have to be saved.
There may also be some differences between C and C++ regarding whether
the result is an lvalue; I don't remember the details.
For C, neither x++ nor ++x can invoke a user-defined function, so the
compiler is better able to optimize the expression. For any decent C
compiler, there should be no difference between x++ and ++x if the
result is discarded (e.g., if it's used as an expression statement).
(The same is probably true in C++ for predefined types.) If the
result *is* used, x++ and ++x are semantically different; choosing the
wrong one will, at best, give you a wrong answer more quickly.