{"id":40,"date":"2020-11-13T05:40:02","date_gmt":"2020-11-13T05:40:02","guid":{"rendered":"https:\/\/codingwithleo.xyz\/?p=40"},"modified":"2023-03-07T22:17:09","modified_gmt":"2023-03-07T22:17:09","slug":"c-lvalues-and-rvalues","status":"publish","type":"post","link":"https:\/\/codingwithleo.xyz\/index.php\/2020\/11\/13\/c-lvalues-and-rvalues\/","title":{"rendered":"C++ lvalues and rvalues"},"content":{"rendered":"\n<p>When we are talking about lvalues and rvalues, it&#8217;s a good idea to back to the C language definition of these words. <strong>In C when we write some code like this:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int x = 1;<\/code><\/pre>\n\n\n\n<p>an <strong>l<\/strong>value is the <strong>left <\/strong>side of the expression and an <strong>r<\/strong>value is the <strong>right <\/strong>side of the expression. We can define it more technically by saying something like: <\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<ul>\n<li><strong>lvalue<\/strong>: is an expression that represents a space on the computer memory, in that space we can store an rvalue.<\/li>\n\n\n\n<li><strong>rvalue<\/strong>: a value that will be stored inside an lvalue. A literal value like 10, 20 or &#8216;a&#8217;.<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<p>Does it look simple, right? Not too fast! It probably would be very simple if we were talking about C, but it&#8217;s not true when talking about C++ because this one brought some concepts that made our life a little bit more complex. In C++ we need to handle classes, constant values and references, so this will modify our whole thinking about rvalues and lvalues.<\/p>\n\n\n\n<p>Starting from the premise that the lvalue must be a space in the memory, it isn&#8217;t possible to read a value that isn&#8217;t in the memory. See the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int* x = &amp;1;<\/code><\/pre>\n\n\n\n<p>A pointer must point to a memory address, as you can see, we are trying to point the pointer <code>x<\/code> to a literal value, so your compiler will tell you the following but in other words: &#8220;the expression on the right side of the code should be an address of some part of your memory&#8221;.<\/p>\n\n\n\n<p>Another pertinent example is about a function that returns a value, take a look at the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int returnVal()\n{\n\treturn 99;\n}\n\nreturnVal() = 10;<\/code><\/pre>\n\n\n\n<p>For obvious reasons we can&#8217;t assign a literal number to a function, for sure it&#8217;s not a common code snippet. Your compiler will tell you something like: &#8220;The expression on the left side of the assignment operator must be an lvalue&#8221;. He is right! We are returning an rvalue through the function and trying to assign a literal to it. But, what if it returns an lvalue?<\/p>\n\n\n\n<p>To make this function return an lvalue we will need to return the value by reference, so, let&#8217;s modify the return type and the return itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">static int Val = 99;\n\nint&amp; returnVal()\n{\n\treturn Val;\n}\n\nreturnVal() = 10;<\/code><\/pre>\n\n\n\n<p>It will compile. Despite <code>returnVal() = 10<\/code> isn&#8217;t a common expression, it will work because the returned value was an lvalue, which means it is allocated in some memory region, so we can assign it.<\/p>\n\n\n\n<p>Maybe you are wondering about a simple expression like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int x = 99;\nint y = 11;\nint z = (x + y);<\/code><\/pre>\n\n\n\n<p>The expression <code>(x+y)<\/code> above (note that I put it inside parentheses just to clarify that it is a complete expression on the right side of the operand <code>=<\/code>. The expression <code>(x+y)<\/code> is an lvalue although it&#8217;s on the right side of the whole expression. Why?<\/p>\n\n\n\n<p>It&#8217;s named lvalue-to-rvalue conversion that means an implicit conversion will be done when some lvalue is assigned to another lvalue. Well, it&#8217;s not too simple but you can find some further information about it in the C++ documentation at the following link: <a rel=\"noreferrer noopener\" href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/implicit_conversion\" target=\"_blank\">Value transformations C++<\/a><\/p>\n\n\n\n<h3>The cherry on top of C++<\/h3>\n\n\n\n<p>Now, after that introduction, we can talk about the const lvalue reference. See the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int&amp; x = 99;<\/code><\/pre>\n\n\n\n<p>As you know <code>x<\/code> is a reference to an lvalue, so it&#8217;s waiting for an lvalue to assign to. The sub expression <code>99<\/code> is a constant number, it&#8217;s not possible to assign it to a reference because it doesn&#8217;t has allocation on the memory, and there&#8217;s no storage reserved for this number.<\/p>\n\n\n\n<p>The reason the code above doesn&#8217;t work is the same reason that the code below won&#8217;t compile.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">void someOperation(int&amp; val)\n{\n\treturn;\n}\n\nsomeOperation(99);<\/code><\/pre>\n\n\n\n<p>In the code we are passing a constant or literal number to a reference function argument. It means that the function was expecting an lvalue and we&#8217;ve passed an rvalue. On the other hand, the code below will work and we will be able to use both, literal values (rvalues) and lvalues. See it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int a = 11;\n\nvoid someOperation(const int&amp; val)\n{\n\treturn;\n}\n\nsomeOperation(99);\nsomeOperation(a);<\/code><\/pre>\n\n\n\n<p>The code above works because using const keyword we solved the problem of the modification of an rvalue, so now we can pass the data by reference even it&#8217;s just a number (rvalue). Why is this necessary? The answer you can find in the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int a = 99;\n\nvoid someOperation(int&amp; val)\t{}\nvoid someOperation(int val)\t{}\n\nsomeOperation(11);\nsomeOperation(a);<\/code><\/pre>\n\n\n\n<p>In the first case <code>someOperation(11)<\/code> the code works because we are passing a literal value, so there&#8217;s just one instance of the function someOperation that matches with the passed argument. But in the second case <code>someOperation(a)<\/code>, the compiler isn&#8217;t able to handle the operation because the value passed is <code>a<\/code> and there are two functions that match with the passed argument.<\/p>\n\n\n\n<p>This problem will block us to use the pass-by-reference technic to reduce the data copy to inside the function. So this way we will get a lower performance than if we were using pass-by-reference.<\/p>\n\n\n\n<p>To handle this situation we need just to use the const operator inside the argument, this way we will secure the pass-by-reference technic and at the same time we can use both, lvalues and rvalues as the argument.<\/p>\n\n\n\n<p>There&#8217;s a huge amount of other things about lvalues and rvalues that it&#8217;s not possible to explain in a single post, so I will try to explain C++ move semantics in another post.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>All the best!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we are talking about lvalues and rvalues, it&#8217;s a good idea to back to the C language definition of these words. In C when we write some code like this: an lvalue is the left side of the expression and an rvalue is the right side of the expression. We can define it more [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":16,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingwithleo.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}