440,159 Members | 1,941 Online
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

# postfix vs prefix operator

 P: n/a Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference Kindly explain. Thanks V.Subramanian Jun 23 '07 #1
8 Replies

 P: n/a su**************@yahoo.com wrote: Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? Because they do. Why do we say that water is wet? I thought both these expressions yield only values. That would be incorrect. There are no such thing as "only values". I am unable to understand the difference You can take an address of an lvalue, you can initialise a reference to-non-const with it, you can apply another increment to it... int i = 10; int *pi = &(++i);// OK int &ri = ++i; // OK ++i--; // OK You can't do those things with an r-value: int i = 10; int *pi = &(i++);// NOT OK int &ri = i++; // NOT OK i++--; // NOT OK What book are you reading that doesn't explain that? V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Jun 23 '07 #2

 P: n/a In article <11*********************@a26g2000pre.googlegroups. com>, su**************@yahoo.com says... Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference From the looks of it, when you say "only values", you basically mean "rvalues". An rvalue is only a value -- something like zero. It doesn't necessarily have any storage associated with it. An lvalue is a variable or something along that general line -- a piece of storage that holds some value at any given time. As the difference between pre- and post-increment, the reason for the difference is fairly simple. In the case of pre-increment, you increment the variable, and the result IS that variable. In the case of post- increment, we increment the variable, but the result is the value the variable held _before_ the increment -- so the result canNOT be held in that variable. This means the result _has_ to be the value independent of the variable -- so we no longer have the address of a variable, and instead have only the value itself. -- Later, Jerry. The universe is a figment of its own imagination. Jun 23 '07 #3

 P: n/a On Jun 24, 12:06 am, Jerry Coffin

 P: n/a In article <11*********************@g37g2000prf.googlegroups. com>, su**************@yahoo.com says... [ ... ] 1) Does it mean that prefix operator in C does not produce Lvalue in C unlike C++ ? That's correct -- in C 6.5.16/3 specifies that: " An assignment expression hs the value of the left operand after the assignment, but is not an lvalue." 6.5.16.2/3 says: "A compound assignment of the form E1 op= E2 differs from the simple assignment E1 = E1 op (E2) only in thta the lvalue E1 is evaluated only once." Finally, 6.5.3.1/2 says: "The expression ++E is equivalent to (E+=1)." and 6.5.3.1/3 says: "The prefix -- operator is analogous to the prefix ++ operator, except that the value of the operand is decremented." So, in C a prefix increment or decrement does no produce an lvalue. 2) I read that, in C++ postfix increment and decrement operator have higher precedence than the prefix operator. But in C they have the same precedence. Am I right or wrong ? You're not exactly right, but not completely wrong either. Neither the C nor C++ standard specifies precedence directly -- in both cases, the precedence must be deduced from the grammar. In both cases, the grammar specifies postfix operators with higher precedence than prefix. In reality, however, this doesn't really matter much -- the only time the relative precedence of a prefix and postfix operator matters is if both are being applied to the same operand, such as "++a++;". In C, since the operators never produce lvalues, and the operand of the prefix operator needs to be an lvalue, you can never have an expression where both the pre- and post-fix operators are applied to the same operand -- so there's never a conflict between the two that you need to resolve via precedence. In C++, for built-in types, the situation is the same. A user-defined type, however, can overload the operators and return modifiable lvalues in both cases: For example: #include class X { int x; public: X(int init = 0) : x(init) {} int &operator++() { return ++x; } int &operator++(int) { return ++x; } operator int() { return x; } }; int main() { X x; ++x++; std::cout << x; return 0; } #include class X { int x; public: X(int init = 0) : x(init) {} int &operator++() { return ++x; } // well-formed but truly evil code: int &operator++(int) { return ++x; } operator int() { return x; } }; int main() { X x; // also well-formed: ++x++; std::cout << (int) x; return 0; } Now, the difference in precedence is meaningful. We're applying both operators to the same operand, and the higher effective precedence of the postfix operator means the "++x++" is equivalent to: x.operator++(int).operator++(); rather than: x.operator++().operator++(int); For built-in types, the postfix operators do not produce modifiable lvalues, so an expression like this is ill-formed, and the difference in precedence is a moot point, just like in C. -- Later, Jerry. The universe is a figment of its own imagination. Jun 24 '07 #5

 P: n/a In article <11*********************@g37g2000prf.googlegroups. com>, su**************@yahoo.com says... [ ... ] 1) Does it mean that prefix operator in C does not produce Lvalue in C unlike C++ ? That's correct -- in C 6.5.16/3 specifies that: " An assignment expression hs the value of the left operand after the assignment, but is not an lvalue." 6.5.16.2/3 says: "A compound assignment of the form E1 op= E2 differs from the simple assignment E1 = E1 op (E2) only in thta the lvalue E1 is evaluated only once." Finally, 6.5.3.1/2 says: "The expression ++E is equivalent to (E+=1)." and 6.5.3.1/3 says: "The prefix -- operator is analogous to the prefix ++ operator, except that the value of the operand is decremented." So, in C a prefix increment or decrement does no produce an lvalue. 2) I read that, in C++ postfix increment and decrement operator have higher precedence than the prefix operator. But in C they have the same precedence. Am I right or wrong ? You're not exactly right, but not completely wrong either. Neither the C nor C++ standard specifies precedence directly -- in both cases, the precedence must be deduced from the grammar. In both cases, the grammar specifies postfix operators with higher precedence than prefix. In reality, however, this doesn't really matter much -- the only time the relative precedence of a prefix and postfix operator matters is if both are being applied to the same operand, such as "++a++;". In C, since the operators never produce lvalues, and the operand of the prefix operator needs to be an lvalue, you can never have an expression where both the pre- and post-fix operators are applied to the same operand -- so there's never a conflict between the two that you need to resolve via precedence. In C++, for built-in types, the situation is the same. A user-defined type, however, can overload the operators and return modifiable lvalues in both cases: For example: #include class X { int x; public: X(int init = 0) : x(init) {} int &operator++() { return ++x; } int &operator++(int) { return ++x; } operator int() { return x; } }; int main() { X x; ++x++; std::cout << x; return 0; } #include class X { int x; public: X(int init = 0) : x(init) {} int &operator++() { return ++x; } // well-formed but truly evil code: int &operator++(int) { return ++x; } operator int() { return x; } }; int main() { X x; // also well-formed: ++x++; std::cout << (int) x; return 0; } Now, the difference in precedence is meaningful. We're applying both operators to the same operand, and the higher effective precedence of the postfix operator means the "++x++" is equivalent to: x.operator++(int).operator++(); rather than: x.operator++().operator++(int); For built-in types, the postfix operators do not produce modifiable lvalues, so an expression like this is ill-formed, and the difference in precedence is moot, just like in C. -- Later, Jerry. The universe is a figment of its own imagination. Jun 24 '07 #6

 P: n/a On Jun 24, 5:12 am, "subramanian10...@yahoo.com, India"

 P: n/a su**************@yahoo.com, India wrote: Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference There is no logical reason for these choices. Just avoid all non-trivial parts of the C++ language. Or avoid the whole language... :-) -- Dr Jon D Harrop, Flying Frog Consultancy The OCaml Journal http://www.ffconsultancy.com/product...ournal/?usenet Jun 26 '07 #8

 P: n/a On Jun 23, 8:22 am, "Victor Bazarov"

### This discussion thread is closed

Replies have been disabled for this discussion.