0

Look at the following code:

int a = 1;
(++a)++;

If I write this code in C souce file, and use gcc to compile it, the result is that:

Increment operator needs lvalue

But if i write this code in c++ source file, use g++ compiler to compile it, there is no error. . According to 《C++ primer 5e》, prefix increment operator returns a lvalue.

So, ++a is a lvalue, why (++a)++ is wrong?

skypjack
  • 49,335
  • 19
  • 95
  • 187
linyuwang
  • 321
  • 1
  • 3
  • 10
  • 3
    Your C++ book says prefix increment operator returns a lvalue. Your C++ compiler compiles it. Your C++ book says nothing about C. What's the problem exactly? – n. m. could be an AI Jul 17 '16 at 07:55
  • why this code in C source file is wrong? – linyuwang Jul 17 '16 at 07:57
  • 1
    Why should it be right? Do you have a C book that says it's right? – n. m. could be an AI Jul 17 '16 at 07:58
  • Does prefix incrementation in C returns a object or lvalue? – linyuwang Jul 17 '16 at 08:01
  • According to the C standard it does not return an lvalue, – n. m. could be an AI Jul 17 '16 at 08:05
  • Could you tell me why C and C++ is so different in prefix incrementation? – linyuwang Jul 17 '16 at 08:08
  • C and C++ are different in operators at all, because, for example, C++ allows overloading and expressions are parsed with other parser. Actually `++a++` works only in C++... So, I think the key of this problem is in expression parsing – VolAnd Jul 17 '16 at 08:12
  • 1
    The C++ standard says in appendix C: "**Change:** The result of a conditional expression, an assignment expression, or a comma expression may be an lvalue. **Rationale:** C++ is an object-oriented language, placing relatively more emphasis on lvalues. For example,functions may return lvalues." The pre-increment operator is defined in terms of compound assignment in both languages so it is covered by this change too. – n. m. could be an AI Jul 17 '16 at 08:25

0 Answers0