0

I came across the following when learning C++

int a = 5;
-----a;

The second statement doesn't compile. The statement could either be read as --(--(-a)) or -(--(--a)), since both operators are in the same precedence group. In this case though only the second interpretation (when you DO use brackets) makes sense. Therefore I see no ambiguity.

My question therefore is: why is the unary negation not in a higher precedence group than the prefix decrement?

Rik Schaaf
  • 1,101
  • 2
  • 11
  • 30
  • 2
    https://en.wikipedia.org/wiki/Maximal_munch - I'm sure this has been asked in some form or another before, including by me for one case. – chris Sep 24 '15 at 01:00
  • Related (or duplicate?): [What does the operation c=a+++b mean?](http://stackoverflow.com/questions/7485088/what-does-the-operation-c-ab-mean) – Yu Hao Sep 24 '15 at 01:05
  • 1
    In reality, look at what you're asking. It's not learning C++. It's about microscopically absurd interpretations of syntax in C. Really, it's saying `-( a-=2)`, because I don't know of any real code (after 30+ years) that would fashion a decrement by 2 as '--(--a)' or '----a', and operator precedence doesn't really focus on such constructs, though there are obfuscation contests I find even more annoying. C++ is about writing understandable code. C had (has) lots of incomprehensible absurdities as part of real life work, but even then readability is far more important than figuring out 5 dashes – JVene Sep 24 '15 at 01:09
  • @chris That is a good explanation. So it is not a language defined issue, but more like a compiler optimalization? Of course we prefer fast compilers over smart compilers :D – Rik Schaaf Sep 24 '15 at 01:16
  • @YuHao it is indeed related, but not a duplicate, since that talks about the postfix de-/increment rather than the prefix version. The postfix version is in a higher precedence group, therefore it is clear what to expect in that situation, if you know the semantics. – Rik Schaaf Sep 24 '15 at 01:20
  • @JVene It is indeed part of my learning process of C++, since in the course I am taking on C++, we are looking at how the compiler interprets the statement. I understand that using the decrement in ----a is not a normal way of decrementing a variable and I will probably never use it in any code, but it is good to know what a compiler does and how it does that when compiling your code. – Rik Schaaf Sep 24 '15 at 01:23
  • 1
    @coolcat007 I get that, I went through it, too. Still, I can't for the life of me grasp why they present oddball scenarios for study of the subject when there's literally billions of lines of code that can provide more worthy examples to peel apart. Now, if the order of precedence in group 3 is strictly followed, with right to left association, the 3 '-' closest to 'a' would parse to (--(-(a)), but won't compile because -a is returning an rvalue (can't decrement it). That continues to the outer -- likewise. --(-a) won't compile for the same reason. Code that can't compile, why study it? – JVene Sep 24 '15 at 02:03
  • 3
    This is probably a much better duplicate: [Why doesn't a+++++b work in C?](http://stackoverflow.com/q/5341202/1708801) and the answers there cover the whole gambit. The answer is the same in C as it is in C++. This has nothing to do with precedence this is a lexical issue. – Shafik Yaghmour Sep 24 '15 at 02:05

0 Answers0