Excerpt from C11 para 6.5.3.3 - item 2:
The result of the unary +
operator is the value of its
(promoted) operand. The integer promotions are performed on the
operand, and the result has the promoted type.
So basically, the effect is almost always a no-op with the exceptions of int
type promotions to its operands. eg,
char a = 6;
size_t size_pre_promotion = sizeof(a);
size_t size_post_promotion = sizeof(+a);
Where because of the type
promotion from char
to int
, the size of the operand a
grew from 1
to 4
. The value of the operand, is returned unchanged.
From comments: "turning lvalue into rvalue", that is what I do not understand...
The effect of placing parenthesis around (+1)
int j = (+i)++;
In this expression (+i)++
, the order of precedence is forced by the parenthesis ()
to perform the transformation of i
(from the expression +i
) from being an lvalue
to an rvalue
, lexically followed by ++
operator in an attempt to increment the value of i
. ( lexically
because this expression will never live beyond compile-time into run-time.) At this point, i
is no longer an lvalue, consequently it is no longer assignable, therefore cannot accept the increment operation that would normally happen if the operand was still an lvalue.
It is interesting to note by way of illustration that the following expressions are perfectly legal:
int j = (+i);//Not attempting to assign value (increment) the operand
//so the _value_ returned from the operand is successfully assigned
//to the lvalue `j`;
int j = +i++;//Assigns the i + 1 to j
//in this case the `++` adds `1` to the operand, then the `+` unary operator returns the incremented value (essentially a no-op at this point), which in turn is assigned to the _lvalue_ `j`.
The unary operator +
turns an lvalue
into an rvalue
.
The terms can essentially be thought of as assignable and not assignable respectively.
As also shown by these expressions:
int j;
&j; //address of an lvalue
is legal. But:
&(+j);//cannot _take_ the address of an rvalue
is not.