{"id":370,"date":"2010-03-10T10:12:57","date_gmt":"2010-03-10T09:12:57","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=370"},"modified":"2010-03-10T10:12:57","modified_gmt":"2010-03-10T09:12:57","slug":"breaking-change-for-rvalue-references-in-visual-studio-2010-rc","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2010\/03\/10\/breaking-change-for-rvalue-references-in-visual-studio-2010-rc\/","title":{"rendered":"Breaking Change for RValue References in Visual Studio 2010 RC"},"content":{"rendered":"<p>The Release Candidate of Visual Studio 2010 has changed the behaviour of RValue references slightly compared to the implementation in the Visual Studio 2010 beta versions. This is because the C++0x standard commitee has changed the RValue reference feature a bit and Visual Studio 2010 RC has incorporated those standard changes. Unfortunately, this might lead to compiler errors when you try to build code that is following the old standard. Let me give an example. Previously using a beta version of Visual Studio 2010 that was using the old C++0x standard, the following code would compile without any problems.<\/p>\n<blockquote>\n<pre>#include &lt;iostream&gt;\r\nusing namespace std;<\/pre>\n<pre>\/\/ Increment value by 1 using RValue reference parameter.\r\nint increment(int&amp;&amp; value)\r\n{\r\n\u00a0\u00a0 cout &lt;&lt; \"value = \" &lt;&lt; value &lt;&lt; endl;\r\n\u00a0\u00a0 value++;\r\n\u00a0\u00a0 return value;\r\n}<\/pre>\n<pre>int main()\r\n{\r\n\u00a0\u00a0 int a = 10;\r\n\u00a0\u00a0 int b = 20;<\/pre>\n<pre>\u00a0\u00a0 \/\/ Increment a\r\n\u00a0\u00a0 int result = increment(a);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;\r\n\u00a0\u00a0 \/\/ Increment b\r\n\u00a0\u00a0 result = increment(b);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;\r\n\u00a0\u00a0 \/\/ Increment an expression\r\n\u00a0\u00a0 result = increment(a + b);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;\r\n\u00a0\u00a0 \/\/ Increment a literal\r\n\u00a0\u00a0 result = increment(3);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;<\/pre>\n<pre>\u00a0\u00a0 return 0;\r\n}\u00a0<\/pre>\n<\/blockquote>\n<p>However, when trying to compile this using the latest release candidate of Visual Studio 2010, you will get the following errors:<\/p>\n<pre>rvalue_test.cpp(18): error C2664: 'increment' : cannot convert parameter 1 from 'int' to 'int &amp;&amp;'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 You cannot bind an lvalue to an rvalue reference\r\nrvalue_test.cpp(21): error C2664: 'increment' : cannot convert parameter 1 from 'int' to 'int &amp;&amp;'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 You cannot bind an lvalue to an rvalue reference<\/pre>\n<p>These are related to the lines that are trying to increment a and b. Incrementing an expression or a literal still works as before. To get rid of those errors, you need to convert the lvalue to an rvalue. You can use the std::move function for this as shown in red\u00a0below.<\/p>\n<blockquote>\n<pre>#include &lt;iostream&gt;\r\nusing namespace std;<\/pre>\n<pre>\/\/ Increment value by 1 using RValue reference parameter.\r\nint increment(int&amp;&amp; value)\r\n{\r\n\u00a0\u00a0 cout &lt;&lt; \"value = \" &lt;&lt; value &lt;&lt; endl;\r\n\u00a0\u00a0 value++;\r\n\u00a0\u00a0 return value;\r\n}<\/pre>\n<pre>int main()\r\n{\r\n\u00a0\u00a0 int a = 10;\r\n\u00a0\u00a0 int b = 20;<\/pre>\n<pre>\u00a0\u00a0 \/\/ Increment a\r\n\u00a0\u00a0 int result = increment(<span style=\"color: #ff0000;\">std::move(a)<\/span>);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;\r\n\u00a0\u00a0 \/\/ Increment b\r\n\u00a0\u00a0 result = increment(<span style=\"color: #ff0000;\">std::move(b)<\/span>);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;\r\n\u00a0\u00a0 \/\/ Increment an expression\r\n\u00a0\u00a0 result = increment(a + b);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;\r\n\u00a0\u00a0 \/\/ Increment a literal\r\n\u00a0\u00a0 result = increment(3);\r\n\u00a0\u00a0 cout &lt;&lt; \"\u00a0 a=\" &lt;&lt; a &lt;&lt; \", b=\" &lt;&lt; b &lt;&lt; \", result=\" &lt;&lt; result &lt;&lt; endl;<\/pre>\n<pre>\u00a0\u00a0 return 0;\r\n}<\/pre>\n<\/blockquote>\n<p>This now compiles without any errors and produces the following output:<\/p>\n<blockquote>\n<pre>value = 10\r\n\u00a0 a=11, b=20, result=11\r\nvalue = 20\r\n\u00a0 a=11, b=21, result=21\r\nvalue = 32\r\n\u00a0 a=11, b=21, result=33\r\nvalue = 3\r\n\u00a0 a=11, b=21, result=4<\/pre>\n<\/blockquote>\n<p>Now, it again works as expected \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Release Candidate of Visual Studio 2010 has changed the behaviour of RValue references slightly compared to the implementation in the Visual Studio 2010 beta versions. This is because the C++0x standard commitee has changed the RValue reference feature a bit and Visual Studio 2010 RC has incorporated those standard changes. Unfortunately, this might lead [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,16,17],"tags":[48,45,55],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-c","category-microsoft","category-software-development","tag-rvalue-references","tag-visual-c-2010","tag-visual-studio-2010"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=370"}],"version-history":[{"count":6,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":376,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/370\/revisions\/376"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}