0

Is it standard-conforming to use expressions like

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

and how the sign of i variable is determined?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

3 Answers3

5

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Turns out it doesn't matter what order they're parsed in because unary negation and positivization are associative for integral types. – Wug Oct 18 '12 at 17:55
  • @Wug in this case, no. It will probably even be optmized out completely. – Luchian Grigore Oct 18 '12 at 17:56
  • 3
    What if they were left-associative? Would it be `((((+-)+)-)+)i`? That's nonsense; i'd not call them right-associative, or in any way associative at all. – anatolyg Oct 18 '12 at 17:57
  • 2
    @anatolyg what if the world was flat? They are right-associative whether you like it or not. – Luchian Grigore Oct 18 '12 at 17:58
  • 1
    Left/right associativity is a property you can assign to binary operators. It makes no sense for unary ones. – avakar Oct 18 '12 at 18:11
  • `+++` parses as `++ +` because of the way lexer works (it always tries to form the longest possible token). I don't see how associativity has anything to do with that. – avakar Oct 18 '12 at 18:25
  • @avakar after further investigation - you're right - but the reference still holds. Actually, this might be a good question. – Luchian Grigore Oct 18 '12 at 18:27
  • Unary operators can be prefix or postfix. You could call all prefix operators "right-associative" and all postfix operators "left-associative", but that would be of little value, and I've never heard those terms used in such a way. As for your reference, note that it is not a quote from the standard, operator precedence and associativity come implicitly from the grammar. – avakar Oct 18 '12 at 18:31
  • @avakar ah - see http://stackoverflow.com/a/12961372/673730 it's a good point. If it were left-associative - `(--x)++` vs `--(x++)`. Or am I missing something? – Luchian Grigore Oct 18 '12 at 18:33
  • Well, that's not associativity, that's precedence. In `++x++`, the two `++` are actually different operators. – avakar Oct 18 '12 at 18:39
  • @avakar yes, saw that in the other answer to the question. – Luchian Grigore Oct 18 '12 at 18:40
1

Your operators has no side effect, +i do nothing with int itself and you do not use the temporary generated value but remove + that do nothing and you have -(-i) witch is equal to i itself.(removing + in the code will convert the operator, I mean remove it in computation because it has no effect)

BigBoss
  • 6,904
  • 2
  • 23
  • 38
  • 1
    If you remove the `+`, you're left with `--i`, which is a whole different story. – Luchian Grigore Oct 18 '12 at 17:57
  • @LuchianGrigore Please read again my answer I already addressed this in end of my answer – BigBoss Oct 18 '12 at 18:00
  • You mean "removing + in the code will convert the operator, I mean remove it in computation because it has no effect"? Yeah... I don't understand that, but if this is what you meant, then ok... – Luchian Grigore Oct 18 '12 at 18:02
0

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.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249