Unless you're a former Fortran programmer, this is obviously a silly thing to do. Suppose you switch the left and right operands around: N is an expression (a subexpression of the assignment expression) referring to an int object. When you use n in an assignment expression such as: For example:ĭeclares n as an object of type int. For the assignment to be valid, the left operand must refer to an object-it must be an lvalue. In other words, the left and right operands of an assignment expression are themselves expressions. The name 'lvalue' comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression." In The C Programming Language (Prentice-Hall, 1988), they wrote " An object is a manipulatable region of storage an lvalue is an expression referring to an object. Kernighan and Ritchie coined the term lvalue to distinguish certain expressions from others. If you program in C++, understanding the built-in operators is essential background for writing well-behaved overloaded operators. But understanding lvalues and rvalues provides valuable insights into the behavior of built-in operators and the code compilers generate to execute those operators. Admittedly, if you program only in C, you can get by without understanding what lvalues and rvalues really are. However, I can explain the underlying concepts common to the standards.Īs is often the case with discussions of esoteric language concepts, it's reasonable for you to ask why you should care. Given the disparity in the definitions for lvalue and rvalue among the language standards, I'm not prepared to offer precise definitions. The 1999 C Standard defines lvalue differently from the 1989 C Standard, and each of those definitions is different from the one in the C++ Standard. (I looked in a dozen books and couldn't find one explanation I liked.) This may be due to of the lack of a consistent definition even among the language standards. Most books on C or C++ do not explain lvalues and rvalues very well. There's also a good chance that you have only a vague understanding of what they are. If you've been programming in either C or C++ for a while, it's likely that you've heard the terms lvalue (pronounced "ELL-value") and rvalue (pronounced "AR-value"), if only because they occasionally appear in compiler error messages. C and C++ enforce subtle differences on the expressions to the left and right of the assignment operator
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |