Several unary operators in C and C++

Is it standard-conforming to use expressions like

int i = 1;
+-+-+i;

and how the sign of i variable is determined?


Yes it is. Unary + and - associate right-to-left, so the expression is parsed as

+(-(+(-(+i))));

Which results in 1 .

Note that these can be overloaded, so for a user-defined type the answer may differ.


你的操作符没有任何副作用, +i对int本身没有做任何事情,你不使用临时生成的值,但删除+什么都不做,并且-(-i)女巫等于i自己。(删除代码中的+将转换运算符,我的意思是在计算中删除它,因为它没有效果)


i isn't modified (C: without intervening sequence points|C++: in an unsequenced manner) so it's legal. You're just creating a new temporary with each operator.

The unary + doesn't even do anything, so all you have is two negations which just give 1 for that expression. The variable i itself is never changed.

链接地址: http://www.djcxy.com/p/12506.html

上一篇: 一元加(+)反对文字字符串

下一篇: C和C ++中的几个一元运算符