{"id":5095,"date":"2016-12-22T18:19:09","date_gmt":"2016-12-22T18:19:09","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/move-semantic-tow-nice-properties\/"},"modified":"2023-06-26T12:31:05","modified_gmt":"2023-06-26T12:31:05","slug":"move-semantic-tow-nice-properties","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/move-semantic-tow-nice-properties\/","title":{"rendered":"Move Semantis: Two Nice Properties"},"content":{"rendered":"<p>I will talk about two nice properties of the move semantic in this post that is not so often mentioned. Containers of the standard template library (STL) can have non-copyable elements. The copy semantic is the fallback for the move semantic. Irritated? I hope so!<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>Moving Instead of Copying<\/h2>\n<p>Do you remember the program<span style=\"font-family: courier new,courier;\"> packagedTask.cpp<\/span> from the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/asynchronous-callable-wrappers\">Asynchronous callable wrappers<\/a>? Of course, not. Here, once more.<\/p>\n<h3>Moving elements in a container<\/h3>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ packagedTask.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;utility&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;future&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;deque&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">SumUp<\/span>{\r\n  public:\r\n    <span style=\"color: #2b91af;\">int<\/span> <span style=\"color: #0000ff;\">operator<\/span>()(<span style=\"color: #2b91af;\">int<\/span> beg, <span style=\"color: #2b91af;\">int<\/span> end){\r\n      <span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">int<\/span> sum{0};\r\n      <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #2b91af;\">int<\/span> i= beg; i &lt; end; ++i ) sum += i;\r\n      <span style=\"color: #0000ff;\">return<\/span> sum;\r\n    }\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  SumUp sumUp1;\r\n  SumUp sumUp2;\r\n  SumUp sumUp3;\r\n  SumUp sumUp4;\r\n\r\n  <span style=\"color: #008000;\">\/\/ define the tasks<\/span>\r\n  std::packaged_task&lt;<span style=\"color: #2b91af;\">int<\/span>(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)&gt; sumTask1(sumUp1);\r\n  std::packaged_task&lt;<span style=\"color: #2b91af;\">int<\/span>(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)&gt; sumTask2(sumUp2);\r\n  std::packaged_task&lt;<span style=\"color: #2b91af;\">int<\/span>(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)&gt; sumTask3(sumUp3);\r\n  std::packaged_task&lt;<span style=\"color: #2b91af;\">int<\/span>(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)&gt; sumTask4(sumUp4);\r\n\r\n  <span style=\"color: #008000;\">\/\/ get the futures<\/span>\r\n  std::future&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; sumResult1= sumTask1.get_future();\r\n  std::future&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; sumResult2= sumTask2.get_future();\r\n  std::future&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; sumResult3= sumTask3.get_future();\r\n  std::future&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; sumResult4= sumTask4.get_future();\r\n\r\n  <span style=\"color: #008000;\">\/\/ push the tasks on the container<\/span>\r\n  std::deque&lt; std::packaged_task&lt;<span style=\"color: #2b91af;\">int<\/span>(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)&gt; &gt; allTasks;\r\n  allTasks.push_back(std::move(sumTask1));\r\n  allTasks.push_back(std::move(sumTask2));\r\n  allTasks.push_back(std::move(sumTask3));\r\n  allTasks.push_back(std::move(sumTask4));\r\n  \r\n  <span style=\"color: #2b91af;\">int<\/span> begin{1};\r\n  <span style=\"color: #2b91af;\">int<\/span> increment{2500};\r\n  <span style=\"color: #2b91af;\">int<\/span> end= begin + increment;\r\n\r\n  <span style=\"color: #008000;\">\/\/ execute each task in a separate thread<\/span>\r\n  <span style=\"color: #0000ff;\">while<\/span> ( not allTasks.empty() ){\r\n    std::packaged_task&lt;<span style=\"color: #2b91af;\">int<\/span>(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)&gt; myTask= std::move(allTasks.front());\r\n    allTasks.pop_front();\r\n    std::<span style=\"color: #0000ff;\">thread<\/span> sumThread(std::move(myTask),begin,end);\r\n    begin= end;\r\n    end += increment;\r\n    sumThread.detach();\r\n  }\r\n\r\n  <span style=\"color: #008000;\">\/\/ get the results<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> sum= sumResult1.get() + sumResult2.get() + sumResult3.get() + sumResult4.get();\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"sum of 0 .. 10000 = \"<\/span> &lt;&lt; sum &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I&#8217;m not interested in the program that calculates the sum of the numbers from 0 .. 10000 in four threads.<\/p>\n<p>I&#8217;m interested in&nbsp;a completely different property of<span style=\"font-family: courier new,courier;\"> std:::packaged_task. std::packaged_task<\/span> is not copyable. The reason is simple. The copy constructor and copy assignment operator are set to <span style=\"font-family: courier new,courier;\">delete<\/span>. You can read the details here: <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/thread\/packaged_task\">cppreference.com<\/a>.<\/p>\n<p>How is it possible to use <span style=\"font-family: courier new,courier;\">std::package_task<\/span> as an element in a container of the STL? Containers of the STL want to own their elements. Therefore, I move via <span style=\"font-family: courier new,courier;\">std::move<\/span> the s<span style=\"font-family: courier new,courier;\">td::package_task<\/span> objects (lines 41 &#8211; 44) into the container<span style=\"font-family: courier new,courier;\"> std::deque.<\/span> Consequently, I must use the move semantic in lines 52 and 54 because I can not copy the <span style=\"font-family: courier new,courier;\">std::package_task.<\/span><\/p>\n<p>But that is not the end of the story. If an algorithm of the STL uses, under the hood, no copy semantics, you can apply it to containers with non-copyable elements. You will get a compiler error if the algorithm uses internal copy semantics.<\/p>\n<\/p>\n<h3>The algorithm on only moveable elements&nbsp;<\/h3>\n<p>In the next example, I make it very simple. I define a simple wrapper <span style=\"font-family: courier new,courier;\">MyInt<\/span> for natural numbers.<span style=\"font-family: courier new,courier;\"><br \/><\/span><span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ moveAlgorithm.cpp<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;numeric&gt; <\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;algorithm&gt; <\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;utility&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;vector&gt;<\/span>\r\n \r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MyInt<\/span>{\r\npublic:\r\n    MyInt(<span style=\"color: #2b91af;\">int<\/span> i_):i(i_){}\r\n    \r\n    <span style=\"color: #008000;\">\/\/ copy semantic<\/span>\r\n    MyInt(<span style=\"color: #0000ff;\">const<\/span> MyInt&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n    MyInt&amp; <span style=\"color: #0000ff;\">operator<\/span>= (<span style=\"color: #0000ff;\">const<\/span> MyInt&amp;)= <span style=\"color: #0000ff;\">delete<\/span>;\r\n    \r\n    <span style=\"color: #008000;\">\/\/ move semantic<\/span>\r\n    MyInt(MyInt&amp;&amp;)= <span style=\"color: #0000ff;\">default<\/span>;\r\n    MyInt&amp; <span style=\"color: #0000ff;\">operator<\/span>= (MyInt&amp;&amp;)= <span style=\"color: #0000ff;\">default<\/span>;\r\n    \r\n    <span style=\"color: #2b91af;\">int<\/span> getVal() <span style=\"color: #0000ff;\">const<\/span> {\r\n        <span style=\"color: #0000ff;\">return<\/span> i;\r\n    }\r\nprivate:\r\n    <span style=\"color: #2b91af;\">int<\/span> i;\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n    std::vector&lt;MyInt&gt; vecMyInt;\r\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> i= 1; i &lt;= 10; ++i){\r\n        vecMyInt.push_back(std::move(MyInt(i)));\r\n    }\r\n    \r\n    std::for_each(vecMyInt.begin(), vecMyInt.end(), [](MyInt&amp; myInt){ std::cout &lt;&lt; myInt.getVal() &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>; });\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n    <span style=\"color: #0000ff;\">auto<\/span> myInt= MyInt(std::accumulate(vecMyInt.begin(), vecMyInt.end(),MyInt(1),[](MyInt&amp; f, MyInt&amp; s){ <span style=\"color: #0000ff;\">return<\/span> f.getVal() * s.getVal(); }));\r\n    \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myInt.getVal(): \"<\/span> &lt;&lt; myInt.getVal() &lt;&lt; std::endl;\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I only declaratively use <span style=\"font-family: courier new,courier;\">delete<\/span> (lines 13 and 14) for the copy semantic and <span style=\"font-family: courier new,courier;\">default<\/span> (lines 17 and 18) for the move semantic. Therefore, the compiler will do the right job for me. I implement only the constructor and the getter <span style=\"font-family: courier new,courier;\">getVal<\/span> (lines 20 &#8211; 22). Although my type is non-copyable, I use it in a <span style=\"font-family: courier new,courier;\">std::for_each<\/span> (line 36) and <span style=\"font-family: courier new,courier;\">std::accumulate<\/span> (line 40) algorithm of the STL.<\/p>\n<p>There are no big surprises.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5093\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/moveAlgorithm.png\" alt=\"moveAlgorithm\" style=\"margin: 15px;\" width=\"362\" height=\"203\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/moveAlgorithm.png 362w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/moveAlgorithm-300x168.png 300w\" sizes=\"auto, (max-width: 362px) 100vw, 362px\" \/><\/p>\n<h4>The program will not compile with cl.exe<\/h4>\n<p>Unfortunately, the program will not compile with a recent cl.exe Compiler (19.10.24807.0 (x86)). Microsoft <span style=\"font-family: Courier New,Courier,monospace;\">std::accumulate <\/span>versions use, under the hood, copy semantics. This is opposite to recent GCC or clang compilers, which use move semantics. But cl.exe is right.<a href=\"http:\/\/en.cppreference.com\/w\/cpp\/algorithm\/accumulate\"> std::accumulate&nbsp;<\/a> requires that T must be copied, assignable, and copy constructible. This will not hold for <span style=\"font-family: Courier;\">MyInt<\/span>. This issue is still under discussion: <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/libstdc%2B%2B\/ext\/lwg-active.html\">https:\/\/gcc.gnu.org\/onlinedocs\/libstdc%2B%2B\/ext\/lwg-active.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The copy semantic is a fallback for the move semantic. What does that mean?<\/p>\n<h2>Copy Semantics as a Fallback for Move Semantics<\/h2>\n<p>If I write an algorithm that internally uses move semantics, I can apply that algorithm to non-copyable types. I have only to change my type<span style=\"font-family: courier new,courier;\"> MyInt.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ copyFallbackMove.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;type_traits&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;utility&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T&gt;\r\n<span style=\"color: #2b91af;\">void<\/span> swapMove(T&amp; a, T&amp; b){\r\n    T tmp(std::move(a));\r\n    a= std::move(b);\r\n    b= std::move(tmp);\r\n}\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MyInt<\/span>{\r\npublic:\r\n    MyInt(<span style=\"color: #2b91af;\">int<\/span> i_):i(i_){}\r\n    \r\n    <span style=\"color: #008000;\">\/\/ copy semantic<\/span>\r\n    MyInt(<span style=\"color: #0000ff;\">const<\/span> MyInt&amp; myInt):i(myInt.getVal()){}\r\n    MyInt&amp; <span style=\"color: #0000ff;\">operator<\/span>= (<span style=\"color: #0000ff;\">const<\/span> MyInt&amp; myInt){\r\n        i= myInt.getVal();\r\n        <span style=\"color: #0000ff;\">return<\/span> *<span style=\"color: #0000ff;\">this<\/span>;\r\n    }\r\n        \r\n    <span style=\"color: #2b91af;\">int<\/span> getVal() <span style=\"color: #0000ff;\">const<\/span> {\r\n        <span style=\"color: #0000ff;\">return<\/span> i;\r\n    }\r\nprivate:\r\n    <span style=\"color: #2b91af;\">int<\/span> i;\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    MyInt myInt1(1);\r\n    MyInt myInt2(2);\r\n    \r\n    std::cout &lt;&lt; std::boolalpha;\r\n    \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_trivially_move_constructible&lt;MyInt&gt;::value \"<\/span> &lt;&lt; std::is_trivially_move_constructible&lt;MyInt&gt;::value &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_trivially_move_assignable&lt;MyInt&gt;::value \"<\/span> &lt;&lt; std::is_trivially_move_assignable&lt;MyInt&gt;::value &lt;&lt; std::endl;\r\n    \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myInt1.getVal() :\"<\/span> &lt;&lt; myInt1.getVal() &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myInt2.getVal() :\"<\/span> &lt;&lt; myInt2.getVal() &lt;&lt; std::endl;\r\n    \r\n    swapMove(myInt1,myInt2);\r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myInt1.getVal() :\"<\/span> &lt;&lt; myInt1.getVal() &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myInt2.getVal() :\"<\/span> &lt;&lt; myInt2.getVal() &lt;&lt; std::endl;\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new,courier;\">MyInt <\/span>has a user-defined copy constructor and copy assignment operator. Therefore, the compiler will not automatically generate a move constructor or move assignment operator. You can read the details here (<a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/move_constructor\">move semantic<\/a>) or ask the type traits library (lines 40 and 41) for help. Sadly, my GCC doesn&#8217;t support this function of the type traits library. So I have to use a more recent GCC 5.2. The critical point is that instances of <span style=\"font-family: courier new,courier;\">MyInt<\/span> can be used in the function template <span style=\"font-family: courier new,courier;\">swapMove<\/span> (lines 7 &#8211; 12), although these instances don&#8217;t support move semantics.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5094\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/copyFallbackMove.png\" alt=\"copyFallbackMove\" width=\"896\" height=\"252\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/copyFallbackMove.png 896w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/copyFallbackMove-300x84.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/copyFallbackMove-768x216.png 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The reason is: A rvalue can be bound to<\/p>\n<ul>\n<li>a constant lvalue reference.<\/li>\n<li>a non-constant rvalue reference.<\/li>\n<\/ul>\n<p>The non-constant rvalue reference has a higher priority than the constant lvalue reference.<span id=\"transmark\"><\/span> A copy constructor or a copy assignment operator expects its arguments as a constant lvalue reference. A move constructor or a move assignment operator expects its arguments as a non-constant rvalue reference. What sounds a little bit confusing has a lovely property.&nbsp;<\/p>\n<h3>Keep the performance in your mind<\/h3>\n<p>You can implement functions like <span style=\"font-family: courier new,courier;\">swapMove<\/span> with move semantics in mind. If your data types are not moveable, the functions will also work for only copyable types. The compiler will use the classic copy semantic as a fallback. Therefore, you can comfortably migrate an old C++ codebase to modern C++. Your program is correct in the first iteration and fast in the second.<\/p>\n<p>&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Writing function temples that can identically forward their arguments, was a &#8221; <em>&#8230; a heretofore unsolved problem in C++.<\/em>&#8220;. (Bjarne Stroustrup). This was because since C++11 <span style=\"font-family: courier new,courier;\">std::forward<\/span> we can use perfect forwarding. Read the details in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\">next post.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will talk about two nice properties of the move semantic in this post that is not so often mentioned. Containers of the standard template library (STL) can have non-copyable elements. The copy semantic is the fallback for the move semantic. Irritated? I hope so!<\/p>\n","protected":false},"author":21,"featured_media":5093,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[498,494],"class_list":["post-5095","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-memory","tag-move"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5095"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5095\/revisions"}],"predecessor-version":[{"id":6909,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5095\/revisions\/6909"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5093"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}