Search code examples
c++ccoding-stylestandardsunary-operator

Why does the C++ standard not deprecate the increment/decrement operators?


I think the following code is evil, but it can be compiled without any warning.

int f(int n)
{
    return n + 1;
}

int n = 0;
n = f(n++) + f(++n);

I just wonder why the Holy Standard doesn't deprecate such operators?

I guess there might be two reasons:

One might be for backward compatibility;

Another might be for that under some cases these operators are very useful.

If the latter is true, could you give me some examples? Thanks.


Solution

  • It's more than evil, it's Undefined Behaviour™. Which is why all sane people ban such uses.

    Backwards compatibility is surely half the problem- increment and decrement are used everywhere. They're also useful for iterators and stuff.

    The bottom line is, C++ has never, ever stopped you from shooting yourself in the foot. This is just one example. C++ does not ban things that can be good just because they can be bad. Not abusing them is your problem.