{"id":2287,"date":"2025-02-15T20:45:03","date_gmt":"2025-02-15T20:45:03","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=2287"},"modified":"2025-04-04T22:44:59","modified_gmt":"2025-04-04T22:44:59","slug":"variables-lvalues-and-rvalues","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/variables-lvalues-and-rvalues\/","title":{"rendered":"Variables, lvalues, and rvalues"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Variables_lvalues_and_rvalues\"><\/span>Variables, lvalues, and rvalues<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Variables\"><\/span>Variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <em>variable<\/em>, sometimes referred to as an <em>object<\/em>, takes up memory space somewhere in a program.<\/p>\n\n\n\n<p>The following statements define variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>int miles_to_go;\ndouble milesPerGallon, price;\nstd::string name;\nVehicle myCar;<\/strong><\/code><\/pre>\n\n\n\n<p>Each variable definition comprises a <em>type specifier<\/em> (<strong>int<\/strong>, <strong>double<\/strong>, etc.), followed by a comma-separated list of one or more names (<strong>miles_to_go<\/strong>, <strong>milesPerGallon<\/strong>, etc.), followed by a semicolon (<strong>;<\/strong>) that terminates the list.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Initialization\"><\/span>Initialization<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>A definition may provide an initialization for the variable. Here are the various ways to initialize a variable:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Direct_Initialization\"><\/span>Direct Initialization<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>Direct initialization uses parentheses, (). It was the standard way to initialize variables before C++11.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int value(42); \/\/ direct-initialization<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Copy_Initialization\"><\/span>Copy Initialization<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>Copy initialization uses an equal sign.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int value = 42; \/\/ copy-initialization<\/code><\/pre>\n\n\n\n<p>One problem with the <em>copy-initialization<\/em> option is that it confuses programmers because it looks like an <em>assignment<\/em>, because it uses the <strong>=<\/strong> operator. It is important for C++ programmers not to confuse <em>initialization<\/em> with <em>assignment<\/em>. As we will see (in particular when we learn about classes), they are not the same.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"List_Initialization\"><\/span>List Initialization<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>List initialization uses braces, {}. It was introduced in C++11, to allow for more consistent and safe initialization.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int value{42}; \/\/ list-initialization<\/code><\/pre>\n\n\n\n<p>This was called <em>aggregate-initialization<\/em> before C++11. Since C++11, it is now called <em>list-initialization<\/em>.<\/p>\n\n\n\n<p>List initialization is now the preferred way to initialize a variable, because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Improved type safety<\/strong> &#8211; generally prevents narrowing conversions, which can lead to unexpected behavior if a value is assigned to a variable of a smaller type.<\/li>\n\n\n\n<li><strong>Readability<\/strong> &#8211; this syntax is often considered more intuitive and easier to read, especially when initializing with multiple values.<\/li>\n\n\n\n<li><strong>Consistency<\/strong> &#8211; this syntax works in most cases, regardless of variable type.<\/li>\n<\/ul>\n\n\n\n<p>If no initializer is specified for an object, the object is default-initialized. If no initializer is specified for a reference, the program is ill-formed.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>There are some cases where a variable that has no initializer is not initialized at all. This means that the program behavior may be undefined &#8212; a common cause of bugs.<\/p>\n<\/blockquote>\n\n\n\n<p>Examples:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;string&gt;\n \nstd::string s1;           \/\/ default-initialization\nstd::string s2();         \/\/ NOT an initialization!\n                          \/\/ actually declares a function \u201cs2\u201d\n                          \/\/ with no parameter and returns std::string\nstd::string s3 = &quot;hello&quot;; \/\/ copy-initialization\nstd::string s4(&quot;hello&quot;);  \/\/ direct-initialization\nstd::string s5{'a'};      \/\/ list-initialization (since C++11)\n \nchar a[3] = {'a', 'b'}; \/\/ aggregate initialization\n                        \/\/ (part of list initialization since C++11)\nchar&amp; c = a[0];         \/\/ reference initialization\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;string&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string s1;<\/span><span style=\"color: #008000\">           \/\/ default-initialization<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">s2<\/span><span style=\"color: #000000\">();<\/span><span style=\"color: #008000\">         \/\/ NOT an initialization!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">                          \/\/ actually declares a function \u201cs2\u201d<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">                          \/\/ with no parameter and returns std::string<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string s3 = <\/span><span style=\"color: #A31515\">&quot;hello&quot;<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #008000\"> \/\/ copy-initialization<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">s4<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;hello&quot;<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\">  \/\/ direct-initialization<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string s5{<\/span><span style=\"color: #A31515\">&#39;a&#39;<\/span><span style=\"color: #000000\">};<\/span><span style=\"color: #008000\">      \/\/ list-initialization (since C++11)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">a<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">] = {<\/span><span style=\"color: #A31515\">&#39;a&#39;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&#39;b&#39;<\/span><span style=\"color: #000000\">};<\/span><span style=\"color: #008000\"> \/\/ aggregate initialization<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">                        \/\/ (part of list initialization since C++11)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&amp; c = <\/span><span style=\"color: #001080\">a<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">];<\/span><span style=\"color: #008000\">         \/\/ reference initialization<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>As you can see, using parentheses is potentially confusing, and using <em>copy-initialization<\/em> is often confused with assignment. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"lvalues_and_rvalues\"><\/span>lvalues and rvalues<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>There are two kinds of expressions in C++:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>lvalue<\/em><\/strong> &#8211; An expression that may appear as either the left-hand or right-hand side of an assignment.<\/li>\n\n\n\n<li><em><strong>rvalue<\/strong><\/em> &#8211; An expression that may appear on the right-hand side of an assignment, but not the left-hand side.<\/li>\n<\/ul>\n\n\n\n<p>Numeric literals are <strong><em>rvalue<\/em><\/strong>s, and so may not be assigned to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int miles_to_go = 37, units = 502;\ndouble price = 42.0, milesPerGallon = 27;\n\nprice * units = total_price; \/\/ error: arithmetic expression is not an lvalue\n0 = 5; \/\/ error: literal constant is not an lvalue<\/code><\/pre>\n\n\n\n<p>You may find that some errors or warnings issued by the compiler use the terms <strong><em>lvalue<\/em><\/strong> and <strong><em>rvalue<\/em><\/strong>, so it important to know their meanings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"lvalue_References_and_rvalue_References\"><\/span>lvalue References and rvalue References<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Prior to C++11, only one type of reference existed in C++, and so it was known as \u201c<em>reference<\/em>\u201d. For better distinction, the ordinary reference <strong>X&amp;<\/strong> is now called an <em>lvalue reference<\/em>.<\/p>\n\n\n\n<p>When <em>lvalues<\/em> were originally defined, they were defined as <em>\u201cvalues that are suitable to be on the left-hand side of an assignment expression\u201d<\/em>. However once the const keyword was added to the C++, <em>lvalues<\/em> were split into \u2014<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>modifiable <em>lvalues<\/em><\/li>\n\n\n\n<li>non-modifiable <em>lvalues<\/em> that are const<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"b2d3\"><span class=\"ez-toc-section\" id=\"Lvalue_references\"><\/span>Lvalue references<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p id=\"ff22\">A reference that <strong>binds to an lvalue,<\/strong> and is<strong> <\/strong>marked with a single ampersand (&amp;). It acts as an alias for the bound object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=========================================================\n \n int x{};\n const int y = 0; \/\/ or const int x {0};\n\n=========================================================\n\n\/\/ l-value references (Table 1)\n int &amp;ref1{ x }; \/\/ OK\n int &amp;ref2{ y }; \/\/ Error\n int &amp;ref3{ 5 }; \/\/ Error\n\n=========================================================\n\n\/\/ lvalue references to const (Table 2)\n const int &amp;ref4{ x }; \/\/ OK\n const int &amp;ref5{ y }; \/\/ OK but can't modify\n const int &amp;ref6{ 5 }; \/\/ OK\n int &amp;ref7{ 5 }; \/\/ Error\n\n=========================================================<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><strong>Table 1<\/strong><\/p>\n\n\n<div class=\"wp-block-tableberg-wrapper is-style-stripes wp-block-tableberg-table\" >\n\t\t\t<div class=\"tableberg-table-wrapper\" style=\"\">\n\t\t\t\t<table class = \"has-inner-border\" style=\"--tableberg-even-bg: #f0f0f0; border-spacing: 0 0; --tableberg-inner-border-top: none; --tableberg-inner-border-right: 1px solid #000000; --tableberg-inner-border-bottom: 1px solid #000000; --tableberg-inner-border-left: none; --tableberg-inner-border-top-first: 1px solid #000000; --tableberg-inner-border-left-first: 1px solid #000000; \" data-tableberg-header=\"converted\" data-tableberg-footer=\"\"  ><colgroup><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><\/colgroup><tbody><tr class=\"tableberg-header\" style=\"\">\n<th data-tableberg-row=\"0\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Type of Values<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>lvalue ref can be initialized<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>lvalue ref can modify<\/p>\n<\/div><\/th>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"1\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-even-row\" style=\"\">\n<td data-tableberg-row=\"2\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Non-modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"3\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><\/tbody><\/table>\n\t\t\t<\/div>\n\t\t<\/div>\n\n\n<p class=\"has-text-align-center\"><strong>Table 2<\/strong><\/p>\n\n\n<div class=\"wp-block-tableberg-wrapper is-style-stripes wp-block-tableberg-table\" >\n\t\t\t<div class=\"tableberg-table-wrapper\" style=\"\">\n\t\t\t\t<table class = \"has-inner-border\" style=\"--tableberg-even-bg: #f0f0f0; border-spacing: 0 0; --tableberg-inner-border-top: none; --tableberg-inner-border-right: 1px solid #000000; --tableberg-inner-border-bottom: 1px solid #000000; --tableberg-inner-border-left: none; --tableberg-inner-border-top-first: 1px solid #000000; --tableberg-inner-border-left-first: 1px solid #000000; \" data-tableberg-header=\"converted\" data-tableberg-footer=\"\"  ><colgroup><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><\/colgroup><tbody><tr class=\"tableberg-header\" style=\"\">\n<th data-tableberg-row=\"0\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Type of Values<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>lvalue ref to const can be initialized<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>lvalue ref to const can modify<\/p>\n<\/div><\/th>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"1\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-even-row\" style=\"\">\n<td data-tableberg-row=\"2\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Non-modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"3\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><\/tbody><\/table>\n\t\t\t<\/div>\n\t\t<\/div>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Rvalue_References\"><\/span>Rvalue References<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p id=\"bdf2\">A reference that <strong>binds to an rvalue<\/strong>, and is marked with two ampersands (&amp;&amp;). It extends the life of the <em>rvalue<\/em> temporary objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=========================================================\n\nint x{};\nconst int y = 0; \/\/ or const int x {0};\n\n=========================================================\n\n\/\/ r-value references (Table 3)\n int &amp;&amp;ref7{ x }; \/\/ Error\n int &amp;&amp;ref8{ y }; \/\/ Error\n int &amp;&amp;ref9{ 5 }; \/\/ OK\n ref9 = 6;        \/\/ OK\n\n=========================================================\n\n\/\/ r-value references to const (Table 4)\n const int &amp;&amp;ref10{ x }; \/\/ Error\n const int &amp;&amp;ref11{ y }; \/\/ Error\n const int &amp;&amp;ref12{ 5 }; \/\/ OK but can't modify\n\n=========================================================<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><strong>Table<\/strong> <strong>3<\/strong><\/p>\n\n\n<div class=\"wp-block-tableberg-wrapper is-style-stripes wp-block-tableberg-table\" >\n\t\t\t<div class=\"tableberg-table-wrapper\" style=\"\">\n\t\t\t\t<table class = \"has-inner-border\" style=\"--tableberg-even-bg: #f0f0f0; border-spacing: 0 0; --tableberg-inner-border-top: none; --tableberg-inner-border-right: 1px solid #000000; --tableberg-inner-border-bottom: 1px solid #000000; --tableberg-inner-border-left: none; --tableberg-inner-border-top-first: 1px solid #000000; --tableberg-inner-border-left-first: 1px solid #000000; \" data-tableberg-header=\"converted\" data-tableberg-footer=\"\"  ><colgroup><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><\/colgroup><tbody><tr class=\"tableberg-header\" style=\"\">\n<th data-tableberg-row=\"0\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Type of Values<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalue ref can be initialized<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalue ref can modify<\/p>\n<\/div><\/th>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"1\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-even-row\" style=\"\">\n<td data-tableberg-row=\"2\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Non-modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"3\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n<\/tr><\/tbody><\/table>\n\t\t\t<\/div>\n\t\t<\/div>\n\n\n<p class=\"has-text-align-center\"><strong>Table 4<\/strong><\/p>\n\n\n<div class=\"wp-block-tableberg-wrapper is-style-stripes wp-block-tableberg-table\" >\n\t\t\t<div class=\"tableberg-table-wrapper\" style=\"\">\n\t\t\t\t<table class = \"has-inner-border\" style=\"--tableberg-even-bg: #f0f0f0; border-spacing: 0 0; --tableberg-inner-border-top: none; --tableberg-inner-border-right: 1px solid #000000; --tableberg-inner-border-bottom: 1px solid #000000; --tableberg-inner-border-left: none; --tableberg-inner-border-top-first: 1px solid #000000; --tableberg-inner-border-left-first: 1px solid #000000; \" data-tableberg-header=\"converted\" data-tableberg-footer=\"\"  ><colgroup><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><col style=\"width: 33.333333333333%; min-width: 33.333333333333%; \"\/><\/colgroup><tbody><tr class=\"tableberg-header\" style=\"\">\n<th data-tableberg-row=\"0\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Type of Values<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalue ref to const can be initialized<\/p>\n<\/div><\/th>\n\n<th data-tableberg-row=\"0\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalue ref to const can modify<\/p>\n<\/div><\/th>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"1\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"1\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-even-row\" style=\"\">\n<td data-tableberg-row=\"2\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Non-modifiable lvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"2\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><tr class=\"tableberg-odd-row\" style=\"\">\n<td data-tableberg-row=\"3\" data-tableberg-col=\"0\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>rvalues<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"1\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>Yes<\/p>\n<\/div><\/td>\n\n<td data-tableberg-row=\"3\" data-tableberg-col=\"2\" style=\"\" class=\"wp-block-tableberg-cell tableberg-v-align-center\"><div class=\"tableberg-cell-inner\" style=\"display: block; justify-content: center; flex-wrap: wrap; \">\n<p>No<\/p>\n<\/div><\/td>\n<\/tr><\/tbody><\/table>\n\t\t\t<\/div>\n\t\t<\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables, lvalues, and rvalues Variables A variable, sometimes referred to as an object, takes up memory space somewhere in a program. The following statements define variables: Each variable definition comprises a type specifier (int, double, etc.), followed by a comma-separated list of one or more names (miles_to_go, milesPerGallon, etc.), followed by a semicolon (;) that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":910,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2287","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=2287"}],"version-history":[{"count":10,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2287\/revisions"}],"predecessor-version":[{"id":2366,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2287\/revisions\/2366"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/910"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=2287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}