{"id":5100,"date":"2016-12-24T15:48:03","date_gmt":"2016-12-24T15:48:03","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\/"},"modified":"2023-06-26T12:30:23","modified_gmt":"2023-06-26T12:30:23","slug":"perfect-forwarding","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\/","title":{"rendered":"Perfect Forwarding"},"content":{"rendered":"<p>Today, we solve &#8221; &#8230; a herefore unsolved problem in C++&#8221; (Bjarne Stroustrup). To make the long story short, I will write about perfect forwarding.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>But what is<strong> perfect forwarding?<br \/><\/strong><\/p>\n<p style=\"padding-left: 30px;\">If a function templates forward its arguments without changing its lvalue or rvalue characteristics, we call it perfect forwarding.<\/p>\n<p>Great. But what are lvalues and rvalues? Now, I have to make a little detour.<\/p>\n<h2>Lvalues and Rvalues<\/h2>\n<p>I will not discuss the details about lvalues and rvalues and introduce, therefore <em>glvalues,<\/em> <em>xvalues<\/em> ,and <em>prvalues. <\/em>That&#8217;s not necessary. In case, you are curious, read the post from Anthony Williams: <a href=\"https:\/\/www.justsoftwaresolutions.co.uk\/cplusplus\/core-c++-lvalues-and-rvalues.html\">Core C++ &#8211; lvalues and rvalues. <\/a>I will provide in my post a sustainable intuition.<\/p>\n<p><strong>Rvalues<\/strong> are<\/p>\n<ul>\n<li>temporary objects.<\/li>\n<li>objects without names.<\/li>\n<li>objects which have no address.<\/li>\n<\/ul>\n<p>If one of the characteristics holds for an object, it will be an rvalue. In reverse, that means that lvalues have a name and an address. Here are a few examples for rvalues:<\/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<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">int<\/span> five= 5;\r\nstd::string a= std::string(<span style=\"color: #a31515;\">\"Rvalue\"<\/span>);\r\nstd::string b= std::string(<span style=\"color: #a31515;\">\"R\"<\/span>) +  std::string(<span style=\"color: #a31515;\">\"value\"<\/span>);\r\nstd::string c= a + b;\r\nstd::string d= std::move(b);<br \/>\r\n<\/pre>\n<\/div>\n<p>Rvalues are on the right side of an assignment. The value 5 and the constructor call are <span style=\"font-family: courier new,courier;\">std::string(&#8220;Rvalue&#8221;)<\/span> rvalues because you can neither determine the address of the value 5 nor has the created string object a name. The same holds for the addition of the rvalues in the expression <span style=\"font-family: courier new,courier;\">std::string(&#8220;R&#8221;) + std::string(&#8220;value&#8221;).<\/span><\/p>\n<p>The addition of the two strings<span style=\"font-family: courier new,courier;\"> a + b<\/span> is interesting. Both strings are lvalues, but the addition creates a temporary object. A particular use case is <span style=\"font-family: courier new,courier;\">std::move(b).<\/span> The new C++11 function converts the lvalue b into an rvalue reference. <span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p>Rvalues are on the right side of an assignment; lvalues can be on the left side of an assignment. But that is not always true:<\/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<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span> five= 5;\r\nfive= 6;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Although variable five is an lvalue. But five is constant, and you can not use it on the left side of an assignment. <span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p>But now to the challenge of this post: Perfect forwarding. To get an intuition for the unsolved problem, I will create a few <em>perfect<\/em> factory methods.<\/p>\n<\/p>\n<h2>A perfect factory method<\/h2>\n<p>First, a short disclaimer. The expression a perfect factory method is no formal term.<\/p>\n<p>A <strong>perfect factory method<\/strong> is, for me, a generic factory method. In particular, that means that the function should have the following characteristics:<\/p>\n<ul>\n<li>It can take an arbitrary number of arguments<\/li>\n<li>Can accept lvalues and rvalues as an argument<\/li>\n<li>Forwards its arguments identical to the underlying constructor<\/li>\n<\/ul>\n<p>I want to say it less formally. A perfect factory method should be able to create each arbitrary object.<\/p>\n<p>Let&#8217;s start with the first iteration.<\/p>\n<h3>First iteration<\/h3>\n<p>For efficiency reasons, the function template should take its arguments by reference. To say it exactly. As a non-constant lvalue reference. Here is the function template <span style=\"font-family: courier new,courier;\">create<\/span> in my first iteration.&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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ perfectForwarding1.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T,<span style=\"color: #0000ff;\">typename<\/span> Arg&gt;\r\nT create(Arg&amp; a){\r\n  <span style=\"color: #0000ff;\">return<\/span> T(a);\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  <span style=\"color: #008000;\">\/\/ Lvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> five=5;\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(five);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive: \"<\/span>  &lt;&lt; myFive &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ Rvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive2= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(5);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive2: \"<\/span> &lt;&lt; myFive2 &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>If I compile the program, I will get a compiler error. The reason is that the rvalue (line 21) can not be bound to a non-constant lvalue reference.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5097\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding1.png\" alt=\"perfectForwarding1\" width=\"700\" height=\"158\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding1.png 1053w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding1-300x68.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding1-1024x230.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding1-768x173.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>Now, I have two ways to solve the issue.<\/p>\n<ol>\n<li>Change the <strong>non-constant lvalue reference<\/strong> (line 6) in a <strong>constant lvalue reference.<\/strong> You can bind an rvalue to a constant lvalue reference. But that is not perfect because the function argument is constant, and I can not change it. <span style=\"font-family: courier new,courier;\"><\/span><\/li>\n<li>Overload the function template for a <strong>constant and <\/strong><strong>non-const lvalue reference.<\/strong> That is easy. That is the right way to go.<\/li>\n<\/ol>\n<h3>Second iteration<\/h3>\n<p>Here is the factory method <span style=\"font-family: courier new,courier;\">create<\/span> overloaded for a constant lvalue reference and a non-constant lvalue reference.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ perfectForwarding2.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T,<span style=\"color: #0000ff;\">typename<\/span> Arg&gt;\r\nT create(Arg&amp; a){\r\n  <span style=\"color: #0000ff;\">return<\/span> T(a);\r\n}\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T,<span style=\"color: #0000ff;\">typename<\/span> Arg&gt;\r\nT create(<span style=\"color: #0000ff;\">const<\/span> Arg&amp; a){\r\n  <span style=\"color: #0000ff;\">return<\/span> T(a);\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  <span style=\"color: #008000;\">\/\/ Lvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> five=5;\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(five);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive: \"<\/span>  &lt;&lt; myFive &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ Rvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive2= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(5);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive2: \"<\/span> &lt;&lt; myFive2 &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>The program produces the expected result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5098\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding2.png\" alt=\"perfectForwarding2\" style=\"margin: 15px;\" width=\"432\" height=\"201\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding2.png 432w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding2-300x140.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/p>\n<p>That was easy. Too easy. The solution has two conceptual issues.<\/p>\n<ol>\n<li>To support n different arguments, I have to overload&nbsp; 2^n +1 variations of the function template <span style=\"font-family: courier new,courier;\">create.<\/span> <span style=\"font-family: courier new,courier;\"><\/span> 2^n +1 because the function <span style=\"font-family: courier new,courier;\">create<\/span> without an argument is part of the perfect factory method.<\/li>\n<li>The function argument mutates in the function body of creating an lvalue, because it has a name. Does this matter? Of course, yes. a is not movable anymore. Therefore, I must perform an expensive copy instead of a cheap move. But what is even worse? If the T (line 12) constructor needs an rvalue, it will no longer work.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Now, I have the solution in the shape of the C++ function <span style=\"font-family: courier new,courier;\">std::forward.<\/span><\/p>\n<h3>Third iteration<\/h3>\n<p>With <span style=\"font-family: courier new,courier;\">std::forward,<\/span> the solution looks promising.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ perfectForwarding3.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T,<span style=\"color: #0000ff;\">typename<\/span> Arg&gt;\r\nT create(Arg&amp;&amp; a){\r\n  <span style=\"color: #0000ff;\">return<\/span> T(std::forward&lt;Arg&gt;(a));\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  <span style=\"color: #008000;\">\/\/ Lvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> five=5;\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(five);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive: \"<\/span>  &lt;&lt; myFive &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ Rvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive2= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(5);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive2: \"<\/span> &lt;&lt; myFive2 &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>Before I present the recipe from <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/utility\/forward\">cppreference.com <\/a>to get perfect forwarding, I will introduce the name universal reference.<\/p>\n<p>The name<strong> universal reference<\/strong> is coined by Scott Meyers.<\/p>\n<p style=\"padding-left: 30px;\">The universal reference (<span style=\"font-family: courier new,courier;\">Arg&amp;&amp; a<\/span>)&nbsp;in line 7 is powerful and can bind lvalues or rvalues. If you declare a variable Arg&amp;&amp; a for a derived type A, you have it at your disposal. <code><\/code><code><\/code><em><\/em><\/p>\n<p>To achieve perfect forwarding, you must combine a universal reference with <span style=\"font-family: courier new,courier;\">std::forward. std::forward&lt;Arg&gt;(a)<\/span> returns the underlying type because a is a universal reference. Therefore, an rvalue remains an rvalue.<\/p>\n<p>Now to the pattern<\/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<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #ff0000;\"><strong>T<\/strong><\/span>&gt;\r\n<span style=\"color: #2b91af;\">void<\/span> wrapper(<strong><span style=\"color: #ff0000;\">T&amp;&amp;<\/span><\/strong> a){\r\n    func(<strong><span style=\"color: #ff0000;\">std::forward&lt;T&gt;(a)<\/span><\/strong>); \r\n}<br \/>&nbsp;<\/pre>\n<\/div>\n<p>I used the color red to emphasize the critical parts of the pattern. I used exactly this pattern in the function template <span style=\"font-family: courier new,courier;\">create.<\/span> Only the name of the type changed from T to <span style=\"font-family: courier new,courier;\">Arg.<\/span><\/p>\n<p>Is the function template <span style=\"font-family: courier new,courier;\">create<\/span> perfect? Sorry to say, but now. <span style=\"font-family: courier new,courier;\">create<\/span> needs precisely one argument, which is perfectly forwarded to the object&#8217;s constructor (line 7). The last step is to make a variadic template out of the function template.<\/p>\n<h3>Fourth iteration &#8211; the perfect factory method<\/h3>\n<p><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/parameter_pack\">Variadic Templates<\/a> are templates that can get an arbitrary number of arguments. That is precisely the missing feature of the perfect factory method.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ perfectForwarding4.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&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, <span style=\"color: #0000ff;\">typename<\/span> ... Args&gt;\r\nT create(Args&amp;&amp; ... args){\r\n  <span style=\"color: #0000ff;\">return<\/span> T(std::forward&lt;Args&gt;(args)...);\r\n}\r\n\r\n<span style=\"color: #0000ff;\">struct<\/span> MyStruct{\r\n  MyStruct(<span style=\"color: #2b91af;\">int<\/span> i,<span style=\"color: #2b91af;\">double<\/span> d,std::string s){}\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  <span style=\"color: #008000;\">\/\/ Lvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> five=5;\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(five);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive: \"<\/span>  &lt;&lt; myFive &lt;&lt; std::endl;\r\n\r\n  std::string str{<span style=\"color: #a31515;\">\"Lvalue\"<\/span>};\r\n  std::string str2= create&lt;std::string&gt;(str);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"str2: \"<\/span> &lt;&lt; str2 &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ Rvalues<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> myFive2= create&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(5);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"myFive2: \"<\/span> &lt;&lt; myFive2 &lt;&lt; std::endl;\r\n\r\n  std::string str3= create&lt;std::string&gt;(std::string(<span style=\"color: #a31515;\">\"Rvalue\"<\/span>));\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"str3: \"<\/span> &lt;&lt; str3 &lt;&lt; std::endl;\r\n\r\n  std::string str4= create&lt;std::string&gt;(std::move(str3));\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"str4: \"<\/span> &lt;&lt; str4 &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #008000;\">\/\/ Arbitrary number of arguments<\/span>\r\n  <span style=\"color: #2b91af;\">double<\/span> doub= create&lt;<span style=\"color: #2b91af;\">double<\/span>&gt;();\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"doub: \"<\/span> &lt;&lt; doub &lt;&lt; std::endl;\r\n  \r\n  MyStruct myStr= create&lt;MyStruct&gt;(2011,3.14,str4);\r\n\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>The three dots in lines 7 -9 are the so-called parameter pack. If the three dots (also called ellipse) are left of <span style=\"font-family: courier new,courier;\">Args,<\/span> the parameter pack will be packed; if right, the parameter pack will be unpacked. In particular, the three dots in line 9 <span style=\"font-family: courier new,courier;\">std std::forward&lt;Args&gt;(args)&#8230;&nbsp;<\/span>causes each constructor call to perform perfect forwarding. The result is impressive. Now, I can invoke the perfect factory method without (line 40) or with three arguments (line 43).<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5099\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding4.png\" alt=\"perfectForwarding4\" style=\"margin: 15px;\" width=\"539\" height=\"254\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding4.png 539w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/perfectForwarding4-300x141.png 300w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>&nbsp;RAII, short for Resource Acquisition Is Initialization is a very important idiom in C++. Why? Read in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/garbage-collectio-no-thanks\">next post.<\/a>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we solve &#8221; &#8230; a herefore unsolved problem in C++&#8221; (Bjarne Stroustrup). To make the long story short, I will write about perfect forwarding.<\/p>\n","protected":false},"author":21,"featured_media":5097,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[494],"class_list":["post-5100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-move"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5100","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=5100"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5100\/revisions"}],"predecessor-version":[{"id":6908,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5100\/revisions\/6908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5097"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}