Yaroslav
Yaroslav

Reputation: 1375

Postfix and prefix increment that causes an error

Why does that code does not compile due to an error:

#include <iostream>

using namespace std;

int main()
{
    int i = 0; 
    cout << ++(i++) << " " << i << endl;
    return 0;
}

While that code does compile:

#include <iostream>

using namespace std;

int main()
{
    int i = 0; 
    cout << (++i)++ << " " << i << endl;
    return 0;
}

I do not understand that. From my point of view it would be pretty reasonable for the first chunk to compile. The expression ++(i++) would just mean take i, increment it and output, then increment it again.

I am not asking about an undefined behavior in int overflow. I do not know about r and l value at all at the time of writing the question and I do not care why is ++i considered an l-value, but i++ is not.

Upvotes: 3

Views: 1312

Answers (3)

Marek Vitek
Marek Vitek

Reputation: 1633

When you compile it with clang you get error message that says it all.

<source>:8:13: error: expression is not assignable
cout << ++(i++) << " " << i << endl;

Maybe it is good to start with ++ operator. In fact it is shorthand for i = i + 1. Now if we look at postfix version i++, it says in standard that it returns copy of original value and as side efect it increments original value. So from (i++) you get rvalue and are trying to assign to it and as we know you can't assign to rvalue.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311078

Shortly speaking the difference is that in C++ you may use any even number of pluses (restricted only by the compiler limits) for the prefix increment operator like this

++++++++++++++++i;

and only two pluses for the post increment operator

i++;

The postfix increment operator returns a value (The C++ Standard, 5.2.6 Increment and decrement)

1 The value of a postfix ++ expression is the value of its operand. [ Note: the value obtained is a copy of the original value —end note ]

While the prefix increment operator returns its operand after increment (The C++ Standard ,5.3.2 Increment and decrement)

1 ...The result is the updated operand; it is an lvalue...

Opposite to C++ in C you also can apply only two pluses to an object using the prefix increment operator.:) Thus the C compiler will issue an error for such an expression like this

++++++++++++++++i;

Upvotes: 2

SergeyA
SergeyA

Reputation: 62603

This is because the post increment and pre increment operators return values with different types. Result of post increment is a so-called 'rvalue', meaning it can not be modified. But pre-increment needs a modifiable value to increment it!

On the other hand, result of pre-increment is an lvalue, meaning that it can be safely modified by the post increment.

And the reason for above rules is the fact that post-increment needs to return a value of the object as it was before increment was applied. By the way, this is why in general case post-incrememts are considered to be more expensive than pre-increments when used on non-builtin objects.

Upvotes: 8

Related Questions