    <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, rvalues, glvalues, prvalues, xvalues, help!</title>
        <link>https://members.accu.org/index.php/articles/2641</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 #150 - April 2019</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/c397/">o150</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+397/">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, rvalues, glvalues, prvalues, xvalues, help!</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 04 April 2019 17:35:48 +01:00 or Thu, 04 April 2019 17:35:48 +01:00</p>
<p><strong>Summary:</strong>&nbsp;C++11 introduced new value categories. Anders Schau Knatten explains his way of thinking about them.</p>
<p><strong>Body:</strong>&nbsp;<p>Did you used to have some sort of intuition for what â€˜lvalueâ€™ and â€˜rvalueâ€™ mean? Are you confused about glvalues, xvalues and prvalues, and worry that lvalues and rvalues might also have changed? This article aims to help you develop a basic intuition for all five of them.</p>

<p>First, a warning: This article does not give a complete definition of the five value categories. Instead, I give a basic outline, which I hope will help to have in the back of your mind the next time you need to look up the actual details of one of them.</p>

<p>Back before C++11, there were two value categories, lvalue and rvalue. The basic intuition was that lvalues were things with identities, such as variables, and rvalues were expressions evaluating to temporaries (with no identity). Consider these definitions:</p>

<pre class="programlisting">
  Widget w;
  Widget getWidget();</pre>
  
<p>If we now use the expression <code>w</code> anywhere, it evaluates to the object <code>w</code>, which has identity. If we use the expression <code>getWidget()</code>, it evaluates to a temporary return value with no identity. Letâ€™s visualise it like this:</p>

<p style="margin-left:1em">
    <img src="http://accu.org/content/images/journals/ol150/Knatten/Knatten-01.png" />
</p>


<p>However, along came rvalue references and move semantics. On the surface, the old lvalue/rvalue distinction seems sufficient: Never move from lvalues (people might still be using them), feel free to move from rvalues (theyâ€™re just temporary anyway). Letâ€™s add movability to our diagram:</p>

<p style="margin-left:1em">
    <img src="http://accu.org/content/images/journals/ol150/Knatten/Knatten-02.png" />
</p>

<p>Why did I put â€˜Canâ€™t moveâ€™ and â€˜lvalueâ€™ in red in that diagram? It turns out that you might want to move from certain lvalues! For instance, if you have a variable you wonâ€™t be using anymore, you can <code>std::move()</code> it to cast it to an rvalue reference. A function can also return an rvalue reference to an object with identity.</p>

<p>So as it turns out, whether something has identity, and whether something can be moved from, are <em>orthogonal properties</em>! Weâ€™ll solve the problem of moving from lvalues soon, but first, letâ€™s just change our diagram to reflect our new orthogonal view of the world:</p>

<p style="margin-left:1em">
    <img src="http://accu.org/content/images/journals/ol150/Knatten/Knatten-03.png" />
</p>

<p>Clearly, thereâ€™s a name missing in the lower left corner here. (We can ignore the top right corner, temporaries which canâ€™t be moved from is not a useful concept.)</p>

<p>C++11 introduces a new value category â€˜xvalueâ€™, for lvalues which can be moved from. It might help to think of â€˜xvalueâ€™ as â€˜eXpiring lvalueâ€™, since theyâ€™re probably about to end their lifetime and be moved from (for instance a function returning an rvalue reference).</p>

<p>In addition, what was formerly called â€˜rvalueâ€™ was renamed to â€˜prvalueâ€™, meaning â€˜pure rvalueâ€™. These are the three basic value categories:</p>

<p style="margin-left:1em">
    <img src="http://accu.org/content/images/journals/ol150/Knatten/Knatten-04.png" />
</p>

<p>But weâ€™re not quite there yet, whatâ€™s a â€˜glvalueâ€™, and what does â€˜rvalueâ€™ mean these days? It turns out that weâ€™ve already explained these concepts! We just havenâ€™t given them proper names yet.</p>

<p style="margin-left:1em">
    <img src="http://accu.org/content/images/journals/ol150/Knatten/Knatten-05.png" />
</p>

<p>A glvalue, or â€˜generalized lvalueâ€™, covers exactly the â€˜has identityâ€™ property, ignoring movability. An rvalue covers exactly the â€˜can moveâ€™ property, ignoring identity. And thatâ€™s it! You now know all the five value categories.</p>

<p>If you want to go into further detail about this topic, cppreference has a very good article [<a href="#[cppreference]">cppreference</a>].</p>

<p class="EditorIntro">This article was first published on Anders Schau Knattenâ€™s blog, <em>C++ on a Friday</em>,  on 9 March 2019 at <a href="https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues-prvalues-xvalues-help/">https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues-prvalues-xvalues-help/</a></p>

<h2>Reference</h2>

<p class="bibliomixed"><a id="[cppreference]"></a>[cppreference] Value categories: <a href="https://en.cppreference.com/w/cpp/language/value_category">https://en.cppreference.com/w/cpp/language/value_category</a></p>


<p class="bio"><span class="author"><b>Anders Schau Knatten</b></span> makes robot eyes at Zivid, where he strives for simplicity, stability and expressive code. Heâ€™s also the author of CppQuiz and @AffectiveCpp, which strive for none of the above.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
