<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Lvalue on Cheat Sheet</title><link>/tags/lvalue/</link><description>Recent content in Lvalue on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/lvalue/index.xml" rel="self" type="application/rss+xml"/><item><title>Expressions</title><link>/programming/cpp/primer/expressions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/expressions/</guid><description>&lt;h1 id="expressions"&gt;Expressions&lt;a class="anchor" href="#expressions"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 4 (Expressions).&lt;/p&gt;
&lt;h2 id="lvalues-and-rvalues"&gt;lvalues and rvalues&lt;a class="anchor" href="#lvalues-and-rvalues"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Every &lt;a href="../../programming/c/syntax-basics/#expression-vs-statement"&gt;expression&lt;/a&gt; in C++ is characterized by two independent properties: its &lt;strong&gt;type&lt;/strong&gt; and its &lt;strong&gt;value category&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id="core-distinction-expression-vs-variable-vs-object"&gt;Core Distinction: Expression vs. Variable vs. Object&lt;a class="anchor" href="#core-distinction-expression-vs-variable-vs-object"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Expression:&lt;/strong&gt; Possesses both a &lt;strong&gt;type&lt;/strong&gt; (reference stripped) and a &lt;strong&gt;value category&lt;/strong&gt; (lvalue, xvalue, prvalue). Value categories are strictly properties of &lt;em&gt;expressions&lt;/em&gt;, never of variables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; Possesses only a &lt;strong&gt;declared type&lt;/strong&gt; (e.g., &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;int&amp;amp;&lt;/code&gt;, &lt;code&gt;int&amp;amp;&amp;amp;&lt;/code&gt;); a variable itself has no value category. When a variable&amp;rsquo;s name appears in code as an expression, that &lt;em&gt;expression&lt;/em&gt; is evaluated as an lvalue (e.g., given &lt;code&gt;int &amp;amp;&amp;amp;rr = 10;&lt;/code&gt;, variable &lt;code&gt;rr&lt;/code&gt; has declared type &lt;code&gt;int&amp;amp;&amp;amp;&lt;/code&gt;, while the expression &lt;code&gt;rr&lt;/code&gt; has type &lt;code&gt;int&lt;/code&gt; and value category lvalue).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object vs. Variable:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Object:&lt;/strong&gt; A region of storage in memory holding a value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; A &lt;strong&gt;named object&lt;/strong&gt; or a &lt;strong&gt;named reference&lt;/strong&gt; introduced by a declaration.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Entity&lt;/th&gt;
 &lt;th style="text-align: center"&gt;Is Object? (Occupies Storage)&lt;/th&gt;
 &lt;th style="text-align: center"&gt;Is Variable? (Has Name)&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Note&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;x&lt;/code&gt; in &lt;code&gt;int x = 10;&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Named object in memory&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;r&lt;/code&gt; in &lt;code&gt;int &amp;amp;r = x;&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Named reference (alias; unlike a pointer, &lt;code&gt;r&lt;/code&gt; has no separate address of its own distinct from &lt;code&gt;x&lt;/code&gt;, though used as an expression &lt;code&gt;r&lt;/code&gt; designates &lt;code&gt;x&lt;/code&gt; and is an lvalue with identity &lt;code&gt;&amp;amp;r == &amp;amp;x&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;10 + 20&lt;/code&gt; (temporary)&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Unnamed temporary object&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="two-axes-of-value-categories"&gt;Two Axes of Value Categories&lt;a class="anchor" href="#two-axes-of-value-categories"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Identity:&lt;/strong&gt; Does the expression have an identifiable memory address (can we test whether two expressions designate the same entity with &lt;code&gt;&amp;amp;&lt;/code&gt;)?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Movable:&lt;/strong&gt; Can the object&amp;rsquo;s resources be safely stolen or moved because it is expiring?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="value-categories-matrix-c11"&gt;Value Categories Matrix (C++11)&lt;a class="anchor" href="#value-categories-matrix-c11"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Non-movable&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Movable&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Has Identity&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;lvalue&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;xvalue&lt;/strong&gt; (&lt;em&gt;eXpiring&lt;/em&gt; value)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;No Identity&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;em&gt;(does not exist)&lt;/em&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;prvalue&lt;/strong&gt; (&lt;em&gt;pure&lt;/em&gt; rvalue)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt; Value Category
 / \
 glvalue rvalue
 / \ / \
 lvalue xvalue prvalue&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="the-5-value-categories"&gt;The 5 Value Categories&lt;a class="anchor" href="#the-5-value-categories"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id="1-primary-categories"&gt;1. Primary Categories&lt;a class="anchor" href="#1-primary-categories"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;lvalue (Left Value):&lt;/strong&gt; Has identity and cannot be moved from. Represents persistent objects or functions in memory.
&lt;ul&gt;
&lt;li&gt;eg. Named variables (&lt;code&gt;x&lt;/code&gt;), dereference expressions (&lt;code&gt;*ptr&lt;/code&gt;), prefix increment/decrement (&lt;code&gt;++i&lt;/code&gt;), function calls returning lvalue references (&lt;code&gt;std::cout &amp;lt;&amp;lt; 1&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;prvalue (Pure Rvalue):&lt;/strong&gt; Has no identity and can be moved from. Represents transient values computed on the fly. In C++17, a prvalue is an evaluation recipe that initializes an object.
&lt;ul&gt;
&lt;li&gt;eg. Numeric/boolean literals (&lt;code&gt;42&lt;/code&gt;, &lt;code&gt;true&lt;/code&gt;, excluding string literals), arithmetic expressions (&lt;code&gt;a + b&lt;/code&gt;), function calls returning by value (&lt;code&gt;GetPoint()&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;xvalue (eXpiring Value):&lt;/strong&gt; Has identity and can be moved from. Designates an object nearing the end of its lifetime whose resources can be safely repurposed.
&lt;ul&gt;
&lt;li&gt;eg. Cast to rvalue reference (&lt;code&gt;std::move(x)&lt;/code&gt; / &lt;code&gt;static_cast&amp;lt;T&amp;amp;&amp;amp;&amp;gt;(x)&lt;/code&gt;), accessing a non-static member of an rvalue (&lt;code&gt;GetPoint().x&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="2-composite-categories"&gt;2. Composite Categories&lt;a class="anchor" href="#2-composite-categories"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;glvalue (Generalized lvalue):&lt;/strong&gt; lvalue + xvalue. Any expression whose evaluation determines the identity of an object or function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;rvalue:&lt;/strong&gt; prvalue + xvalue. Any expression eligible for move semantics.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="example"&gt;Example&lt;a class="anchor" href="#example"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;string a &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0a3069"&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;string b &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0a3069"&gt;&amp;#34;world&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;a&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// lvalue: has persistent identity, cannot be moved from
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;a &lt;span style="color:#0550ae"&gt;+&lt;/span&gt; b&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// prvalue: transient computed value without persistent identity
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;move&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;a&lt;span style="color:#1f2328"&gt;);&lt;/span&gt; &lt;span style="color:#57606a"&gt;// xvalue: designates existing object &amp;#39;a&amp;#39;, but marked movable
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="decltype-two-step-deduction-rules-and-expressions"&gt;&lt;code&gt;decltype&lt;/code&gt; Two-Step Deduction Rules and Expressions&lt;a class="anchor" href="#decltype-two-step-deduction-rules-and-expressions"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;decltype(...)&lt;/code&gt; evaluates its operand using a &lt;strong&gt;two-step rule (Rule 1 → Rule 2)&lt;/strong&gt;:&lt;/p&gt;</description></item></channel></rss>