    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Lvalues and Rvalues</title>
        <link>https://members.accu.org/index.php/articles/227</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #61 - Jun 2004</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c151/">61</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-151/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+151/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Lvalues and Rvalues</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 01 June 2004 16:51:14 +01:00 or Tue, 01 June 2004 16:51:14 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>The two concepts, <i class="firstterm">lvalue</i> and <i class=
"firstterm">rvalue</i>, can be somewhat confusing in C++.
Nevertheless, the difference is important to understand. The basic
consequences related to these concepts are interesting and good to
know in many cases.</p>
<p>In C++ every expression yields either an lvalue or an rvalue and
accordingly every expression is called either an lvalue or an
rvalue expression. An example of an lvalue is an identifier. As a
further example, a reference to an object is an lvalue. Every
expression that is not an lvalue is an rvalue. A good example of
this is an expression that produces an arithmetic value. An
intuitive approach would be to think of expressions as functions
and then an lvalue can be thought as the result of a function
returning a reference.</p>
<p>Examples:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>The subscript operator is a function of the form <tt class=
"literal">T&amp; operator[](T*, ptrdiff_t)</tt> and therefore
<tt class="literal">A[0]</tt> is an lvalue where <tt class=
"literal">A</tt> is of array type.</p>
</li>
<li>
<p>The dereference operator is a function of the form <tt class=
"literal">T&amp; operator*(T*)</tt> and hence <tt class=
"literal">*p</tt> is an lvalue where <tt class="literal">p</tt> is
of pointer type.</p>
</li>
<li>
<p>The negate operator is of the form <tt class="literal">T
operator-(T)</tt> and therefore <tt class="literal">-x</tt> is an
rvalue.</p>
</li>
</ol>
</div>
<p>The terms lvalue and rvalue were inherited from C. The original
meaning comes from the assignment: an lvalue being the left side of
the assignment and rvalue the right side. However, in the modern
C++, lvalue can be considered more as a <i class=
"firstterm">locator value</i>. An lvalue refers to a defined region
of storage. Although, this is not true with the function lvalues
since functions are not objects. Similarly, an rvalue can be
considered as a value of an expression. This separation of two
concepts helps to define and talk about things, though some would
say that it has caused more confusion. Nevertheless, exact
definitions are needed to clarify a language.</p>
<p>An rvalue should not be confused with the constness of an
object. An rvalue does not mean the object would be immutable.
There is some confusion about this, since non-class rvalues are
non-modifiable. This is not the case with user types. A class
rvalue can be used to modify an object through its member
functions. Albeit in practice, it can be said that objects are
modified only through <i class="firstterm">modifiable lvalues</i>.
A modifiable lvalue is an lvalue that can be used to modify the
object. Other lvalues are non-modifiable lvalues, const reference
is a good example of this.</p>
<p>As mentioned already, non-class rvalues do not have the same
qualities as the user type rvalues. One might wonder about this.
After all, C++ was designed so that user types would behave like
built-ins, at least as uniformly as possible. Still this
inconsistency exists and the reasons for it shall be explored
later. Non-class rvalues are not modifiable, nor can have
cv-qualified types (the cv-qualifications are ignored). On the
contrary, the class rvalues are modifiable and can be used to
modify an object via its member functions. They can also have
cv-qualified types. In case of built-ins, some operators require an
lvalue as does every assignment expression as the left side. The
built-in address-of operator requires an lvalue which reflects the
character of lvalues rather well.</p>
<p>Examples:</p>
<pre class="programlisting">
int var = 0;
var = 1 + 2; // ok, var is an lvalue here
var + 1 = 2 + 3; // error, var + 1 is an rvalue
int* p1 = &amp;var; // ok, var is an lvalue
int* p2 = &amp;(var + 1); // error, var + 1 is an rvalue
UserType().member_function(); // ok, calling a
                              // member function of the class rvalue
</pre>
<p>The only real reason I can think of for needing the class
rvalues to be modifiable, is to allow the calling of the nonconst
members of the proxies<sup>[<a name="d0e84" href="#ftn.d0e84" id=
"d0e84">1</a>]</sup> and similar. That is, a proxy represents a
type and it ought to behave accordingly. Keeping this in mind when
considering coherent behaviour along with the const-correctness,
one would make the mutating members non-const. For this to work the
modifiable rvalues are needed. Although this may seem quite
irrelevant, it is actually quite an important reason. It can be
used to emulate lvalue behaviour and hence many things are made
possible. Thinking about the modifiable class rvalues more closely,
they enable many usable concepts and there are only a few rare
cases when that introduces problems. After all, the big difference
between the built-in types and the user types is that the user
types can have members. This difference effectively makes the
non-class rvalues non-modifiable.</p>
<p>An rvalue cannot be used to initialise non-const reference. That
is, an rvalue cannot be converted to an lvalue, but when an lvalue
is used in a context where an rvalue is expected, the lvalue is
implicitly converted to an rvalue. This binding restriction and the
modifiable class rvalue lead to interesting consequences. It allows
us to call all member functions for a user type but not mutating
free functions. This can be confusing as one needs to know whether
an operator is a member or not, and after all it is not consistent.
The same problem motivates us to implement operators as non-members
where possible, for consistency with built-in types. Also, since
the member functions can be called, the called function can return
a non-const reference to the object itself. This means that a
modifiable lvalue referring to a temporary object can be created,
making it possible to call a function that takes a non-const
reference. Time has shown this to be very dangerous as it allows
mistakes that can be hard to find, mostly because of the implicit
conversions. That ought not to be the case here, since it is not
easy to make such a mistake by accident.</p>
<p>Examples:</p>
<pre class="programlisting">
struct A {
A&amp; operator=(const A&amp;) { return *this; }
};
void func(A&amp;);
..
func(A() = A()); // fine, operator= yields an lvalue
ofstream(&quot;some&quot;) &lt;&lt; some_variable; // fine as long
// as the operator&lt;&lt; is a member
</pre>
<p>Forbidding the binding of rvalues to non-const references does
not come without problems, however. It makes it difficult to
provide uniform behaviour with unusual copy semantics, like those
of <tt class="classname">auto_ptr</tt>, which is why they are a bad
idea. This is why a special reference has been proposed for the
next C++ standard. It would only bind to an rvalue. The proposal
actually makes a distinction between the rvalue and the lvalue
references by introducing a new syntax. This would allow detection
of an rvalue which is crucial for the move semantics<sup>[<a name=
"d0e99" href="#ftn.d0e99" id="d0e99">2</a>]</sup>. The proposed
resolution would effectively allow the uniform behaviour but still
not compromise on the safety issues.</p>
<p>The biggest problem with non-consistency seems to be the
confusion among the people. Of course it would be desirable to be
consistent in the eyes of purists but you cannot always get it all.
After taking a little trouble to understand, an lvalue and an
rvalue are quite easy concepts.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e105" id=
"d0e105"></a>Acknowledgement</h2>
</div>
<p>I would like to thank Rani Sharoni for providing helpful
comments.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2>References and
Related Links</h2>
</div>
<div class="bibliomixed"><a name="ISO14882" id="ISO14882"></a>
<p class="bibliomixed">[ISO14882] ISO/IEC 14882-1998 Standard for
the C++ language</p>
</div>
<div class="bibliomixed"><a name="ISO9899" id="ISO9899"></a>
<p class="bibliomixed">[ISO9899] ISO/IEC 9899-1999 Programming
language C</p>
</div>
<div class="bibliomixed"><a name="Koenig-" id="Koenig-"></a>
<p class="bibliomixed">[Koenig-] Andrew Koenig and Barbara E. Moo,
<span class="citetitle"><i class="citetitle">Accelerated C++,
Practical Programming by Example</i></span>, 2000, Addison
Wesley</p>
</div>
<div class="bibliomixed"><a name="Stroustrup" id="Stroustrup"></a>
<p class="bibliomixed">[Stroustrup] Bjarne Stroustrup, <span class=
"citetitle"><i class="citetitle">The Design and Evolution of
C++</i></span>, 1994, Addison Wesley</p>
</div>
<div class="bibliomixed"><a name="Gamma-" id="Gamma-"></a>
<p class="bibliomixed">[Gamma-] Erich Gamma, Richard Helm, Ralph
Johnson, John Vlissides, <span class="citetitle"><i class=
"citetitle">Design Patterns: Elements of Reusable Object-Oriented
Software</i></span>, 1995, Addison Wesley</p>
</div>
<div class="bibliomixed"><a name="Hinnant-" id="Hinnant-"></a>
<p class="bibliomixed">[Hinnant-] Howard E. Hinnant, Peter Dimov
and Dave Abrahams, <span class="citetitle"><i class="citetitle">A
Proposal to Add Move Semantics Support to the C++
Language</i></span>, 2002, <span class="bibliomisc"><a href=
"http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1377.htm"
target=
"_top">http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1377.htm</a></span></p>
</div>
<div class="bibliomixed"><a name="auto_ptr" id="auto_ptr"></a>
<p class="bibliomixed">[auto_ptr] auto_ptr update, Scott Meyers,
<span class="bibliomisc"><a href=
"http://www.awprofessional.com/content/images/020163371X/autoptrupdate%5Cauto_ptr_update.html"
target=
"_top">http://www.awprofessional.com/content/images/020163371X/autoptrupdate%5Cauto_ptr_update.html</a></span></p>
</div>
<div class="bibliomixed"><a name="Alexandrescu" id=
"Alexandrescu"></a>
<p class="bibliomixed">[Alexandrescu] Generic&lt;Programming&gt;:
Move Constructors, Andrei Alexandrescu, <span class=
"bibliomisc">http://www.cuj.com/documents/s=8246/cujcexp2102alexandr/alexandr.htm [Editor's Note: This link is broken; C/C++ User's Journal is out of business]</span></p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c2" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e84" href="#d0e84" id=
"ftn.d0e84">1</a>]</sup> Proxy: provide a surrogate or placeholder
for another object to control access to it.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e99" href="#d0e99" id=
"ftn.d0e99">2</a>]</sup> A proposal to add support for move
semantics to the C++ has been made and the rvalue-reference
proposal is part of it.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
