{"id":6160,"date":"2021-06-07T21:13:00","date_gmt":"2021-06-07T21:13:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/template-arguments\/"},"modified":"2021-06-07T21:13:00","modified_gmt":"2021-06-07T21:13:00","slug":"template-arguments","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/template-arguments\/","title":{"rendered":"Template Arguments"},"content":{"rendered":"<p>It is pretty interesting how the compiler deduces the types for the template arguments. To make it short, you get most of the time the type you expect. The rules do not only apply to function templates (C++98) but also to&nbsp;<code>auto<\/code> (C++11), to class templates (C++17), and concepts (C++20).<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6152\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesArguments.png\" alt=\"templatesArguments\" width=\"650\" height=\"392\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesArguments.png 909w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesArguments-300x181.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesArguments-768x463.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>C++ has supported function template argument deduction since its beginning. Here is a short recap.<\/p>\n<h2>Function Template Argument Deduction<\/h2>\n<p>Let me invoke a function template <code>max<\/code> for <code>int <\/code>and double<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\nT max(T lhs, T rhs) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> (lhs <span style=\"color: #555555;\">&gt;<\/span> rhs)<span style=\"color: #555555;\">?<\/span> lhs <span style=\"color: #555555;\">:<\/span> rhs;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n  \r\n    max(<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">5<\/span>);        <span style=\"color: #3366ff;\"> \/\/ (1)<\/span>\r\n    max(<span style=\"color: #ff6600;\">10.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);    <span style=\"color: #3366ff;\"> \/\/ (2)<\/span>\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In this case, the compiler deduces the template arguments from the function arguments.&nbsp;<a href=\"https:\/\/cppinsights.io\/lnk?code=dGVtcGxhdGUgPHR5cGVuYW1lIFQ+ClQgbWF4KFQgbGhzLCBUIHJocykgewogICAgcmV0dXJuIChsaHMgPiByaHMpPyBsaHMgOiByaHM7Cn0KCmludCBtYWluKCkgewogIAogICAgbWF4KDEwLCA1KTsKICAgIG1heCgxMC41LCA1LjUpOwogIAp9&amp;insightsOptions=cpp2a&amp;std=cpp2a&amp;rev=1.0\">C++ Insights<\/a> shows that the compiler creates a fully specialized function template for <code>max<\/code> for<code> int<\/code> (1) and for <code>double<\/code> (2).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6128\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTempateIntDouble.png\" alt=\"functionTempateIntDouble\" width=\"395\" height=\"545\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTempateIntDouble.png 395w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTempateIntDouble-217x300.png 217w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/p>\n<p>The process of template type deduction, such as in this case, produces most of the time the expected type. It is pretty enlightening to analyze this process deeper.<\/p>\n<\/p>\n<h2>Template Type Deduction<\/h2>\n<p>When deducing the template type, three entities come into play: T, ParameterType, and expression.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func(ParameterType param);\r\n\r\nfunc(expression);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Two types are deduced:<\/p>\n<ul>\n<li><code>T<\/code><\/li>\n<li><code>ParameterType<\/code><\/li>\n<\/ul>\n<p>The <code>ParameterType<\/code> can be a<\/p>\n<ul>\n<li>Value<\/li>\n<li>Reference (&amp;) or Pointer (*)<\/li>\n<li>Universal Reference (&amp;&amp;)<\/li>\n<\/ul>\n<p>The <code>expression<\/code> can be an lvalue or an rvalue having. Additionally, the lvalue or rvalue can be a reference, or <code>const<\/code>\/<code>volatile<\/code> qualified.<\/p>\n<p>The easiest way to understand the template type deduction process is to vary the<code> ParameterType<\/code>.<\/p>\n<h3>ParameterType is a Value<\/h3>\n<p>Taking the parameter by value is probably the most used variant.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func(T param);\r\n\r\nfunc(expr);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>When <code>expr<\/code> is a reference, the reference is ignored <code>=&gt; newExpr <\/code>is created<\/li>\n<li>When <code>newExpr<\/code> is <code>const<\/code> or <code>volatile<\/code>, <code>const<\/code> or <code>volatile<\/code> is ignored.<\/li>\n<\/ul>\n<p>If the parameter type is a reference or a universal reference, the constness (or volatileness) of <code>expr<\/code> is respected.<\/p>\n<h3>ParameterType is a Reference (&amp;) or Pointer (*)<\/h3>\n<p>For simplicity, I use a reference. The analogous argumentation holds for a pointer. Essentially, you get precisely the result you expect.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func(T<span style=\"color: #555555;\">&amp;<\/span> param);\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ void func(T* param);<\/span>\r\n\r\nfunc(expr);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>When <code>expr<\/code> is a reference, the reference is ignored (but added at the end).<\/li>\n<li>The expr matches the <code>ParameterType<\/code> and the resulting type becomes a reference. This means,\n<ul>\n<li>an <code>expr<\/code> of type <code>int<\/code> becomes an<code> int&amp;<\/code><\/li>\n<li>an <code>expr<\/code> of type<code> const int<\/code> becomes a <code>const int&amp;<\/code><\/li>\n<li>an <code>expr<\/code> of type<code> const int&amp;<\/code> becomes a <code>const int&amp;<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>ParameterType is a Universal Reference (&amp;&amp;)<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func(T<span style=\"color: #555555;\">&amp;&amp;<\/span> param);\r\n\r\nfunc(expr);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>When <code>expr<\/code> is an lvalue, the resulting type becomes an lvalue reference.<\/li>\n<li>When <code>expr<\/code> is an rvalue, the resulting type becomes an rvalue reference.<\/li>\n<\/ul>\n<p>Admittedly, this explanation was pretty technical. Here is an example.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ templateTypeDeduction.cpp<\/span>\r\n<span style=\"color: #009999;\"><\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> funcValue(T param) { }\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> funcReference(T<span style=\"color: #555555;\">&amp;<\/span> param) { }\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> funcUniversalReference(T<span style=\"color: #555555;\">&amp;&amp;<\/span> param) { }\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">RVal<\/span>{};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> lVal{};\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> ref <span style=\"color: #555555;\">=<\/span> lVal;\r\n  \r\n    funcValue(lVal);                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    funcValue(ref);\r\n  \r\n    funcReference(lVal);              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  \r\n    funcUniversalReference(lVal);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    funcUniversalReference(RVal());\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I define and use a function template taking its argument by value (1), by reference (2), and by universal reference (3).<\/p>\n<p>Thanks to<a href=\"https:\/\/cppinsights.io\/s\/6bb71783\"> C++ Insights<\/a>, I can visualize the type deduction of the compiler.<\/p>\n<ul>\n<li><strong>(1)<\/strong>: Both calls of <code>funcValue<\/code> cause the same instantiation of the function template. The deduced type is an <code>int<\/code>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6153\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionValue.png\" alt=\"TypeDeductionValue\" width=\"400\" height=\"127\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionValue.png 405w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionValue-300x95.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<ul>\n<li><strong>(2)<\/strong>: Calling the function <code>funcReference<\/code> with<code> const int&amp;<\/code> gives the type<code> const int&amp;<\/code>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6154\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionReference.png\" alt=\"TypeDeductionReference\" width=\"436\" height=\"128\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionReference.png 436w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionReference-300x88.png 300w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><\/p>\n<ul>\n<li><strong>(3)<\/strong>: Using the function <code>funcUniversalReference<\/code> give an lvalue reference or an rvalue reference.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6155\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionUniversalReference.png\" alt=\"TypeDeductionUniversalReference\" width=\"476\" height=\"336\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionUniversalReference.png 476w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TypeDeductionUniversalReference-300x212.png 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/p>\n<p>There is one interesting fact when you invoke the function <code>funcValue<\/code> with a C-array. The C-array decays.<\/p>\n<h3>Decay of a C-array<\/h3>\n<p>Taking a C-array by value is remarkable.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ typeDeductionArray.cpp<\/span>\r\n<span style=\"color: #009999;\"><\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> funcValue(T param) { }\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> intArray[<span style=\"color: #ff6600;\">10<\/span>]{ <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>};\r\n\r\n    funcValue(intArray);\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you invoke the function template <code>funcValue<\/code> with a C-array, the C-array decays to a pointer onto its first element. Decay has many facets. It is applied when a function argument is passed by value. Decay means that an implicit conversion function-to-pointer, array-to-pointer, or lvalue-to-rvalue is applied. Additionally, the reference of a type T and its const-volatile qualifiers are removed.<\/p>\n<p>Here is the screenshot of the program from <a href=\"https:\/\/cppinsights.io\/s\/910a53e4\">C++ Insights<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6156\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArray.png\" alt=\"typeDeductionArray\" width=\"359\" height=\"129\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArray.png 359w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArray-300x108.png 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/p>\n<p>This essentially means that you don&#8217;t know the size of the C-array.&nbsp;<\/p>\n<p>But there is a trick. Taking the C-array by reference and pattern matching on the type and the size on the C-array gives you the size of the C-array:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ typeDeductionArraySize.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstddef&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> N<span style=\"color: #555555;\">&gt;<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> funcArraySize(T (<span style=\"color: #555555;\">&amp;<\/span>arr)[N]) { \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> N;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> intArray[<span style=\"color: #ff6600;\">10<\/span>]{ <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>};\r\n\r\n    funcArraySize(intArray);\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"funcArraySize(intArray): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> funcArraySize(intArray) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function template <code>funcArraySize<\/code> deduces the size of the C-arrays. I gave, for readability reasons, the C-array parameter the name<code> arr: std::size_t funcArraySize(T (&amp;arr)[N])<\/code>. This is not necessary, and you can write <code>std::size_t funcArraySize(T (&amp;)[N])<\/code>.&nbsp; Here are the internals from<a href=\"https:\/\/cppinsights.io\/s\/6e908572\"> C++ Insights<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6157\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArraySize.png\" alt=\"typeDeductionArraySize\" width=\"412\" height=\"151\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArraySize.png 412w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArraySize-300x110.png 300w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/p>\n<p>&nbsp;Finally, the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6158\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArraySizeProgram.png\" alt=\"typeDeductionArraySizeProgram\" width=\"450\" height=\"210\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArraySizeProgram.png 461w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/typeDeductionArraySizeProgram-300x140.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>When you understand template type deduction, you essentially understand <code>auto<\/code> type deduction in C++11.<\/p>\n<h2><code>auto<\/code> Type Deduction<\/h2>\n<p><code>auto<\/code> type deduction uses the rules of template type deduction.<\/p>\n<p>To remind you, these are the essential entities of template type deduction:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span> \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func(ParameterType param);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> val <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2011<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Understanding <code>auto<\/code> means that you have to regard <code>auto<\/code> as the replacements for<code> T<\/code> and the type specifiers of <code>auto<\/code> as the replacements for the <code>ParameterType<\/code> in the function template.&nbsp;<\/p>\n<p>The type specifier can be a value (1), a reference (2), or a universal reference (3).<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">auto<\/span> val <span style=\"color: #555555;\">=<\/span> arg;      <span style=\"color: #006699;\">\/\/ (1)<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;<\/span> val <span style=\"color: #555555;\">=<\/span> arg;     <span style=\"color: #006699;\">\/\/ (2)<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;&amp;<\/span> val <span style=\"color: #555555;\">=<\/span> arg;  <span style=\"color: #006699;\">  \/\/ (3)\r\n<\/span><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s try it out and change the previous program <code>templateTypeDeduction.cpp<\/code> and use <code>auto<\/code> instead of function templates.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ autoTypeDeduction.cpp<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">RVal<\/span>{};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> lVal{};\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> ref <span style=\"color: #555555;\">=<\/span> lVal;\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> val1 <span style=\"color: #555555;\">=<\/span> lVal;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> val2 <span style=\"color: #555555;\">=<\/span> ref;\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;<\/span> val3 <span style=\"color: #555555;\">=<\/span> lVal;         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;&amp;<\/span> val4 <span style=\"color: #555555;\">=<\/span> lVal;        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;&amp;<\/span> val5 <span style=\"color: #555555;\">=<\/span> RVal();\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you study the resulting types in<a href=\"https:\/\/cppinsights.io\/s\/2c652b47\"> C++ Insights<\/a>, you see that they are identical to the types deduced in the program <code>templateTypeDeduction.cpp<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6159\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/autoTypeDeduction.png\" alt=\"autoTypeDeduction\" width=\"266\" height=\"335\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/autoTypeDeduction.png 266w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/autoTypeDeduction-238x300.png 238w\" sizes=\"auto, (max-width: 266px) 100vw, 266px\" \/><\/p>\n<p>Of course, <code>auto<\/code> decays when it takes a C-array by value.<\/p>\n<div class=\"\" dir=\"auto\">\n<div id=\"jsc_c_69\" class=\"ecm0bbzt hv4rvrfc ihqw7lf3 dati1w0a\" data-ad-comet-preview=\"message\" data-ad-preview=\"message\">\n<div class=\"j83agx80 cbu4d94t ew0dbk1b irj2b8pg\">\n<div class=\"qzhwtbm6 knvmm38d\">\n<div class=\"kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x c1et5uql ii04i59q\">\n<h2 dir=\"auto\">The New pdf-Bundle is Ready: C++20 Coroutines<\/h2>\n<\/div>\n<div class=\"o9v6fnle cxmmr5t8 oygrvhab hcukyx3x c1et5uql ii04i59q\">\n<div dir=\"auto\">I have prepared the pdf-bundle. To get it is pretty simple. If you subscribe to my German or English newsletter, you will get the link to the pdf bundle.<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<div class=\"o9v6fnle cxmmr5t8 oygrvhab hcukyx3x c1et5uql ii04i59q\">\n<div dir=\"auto\">Here is more information about the pdf-Bundle:&nbsp;<a href=\"https:\/\/www.modernescpp.com\/index.php\/the-new-pdf-bundle-is-ready-coroutines\"> C++ Coroutines<\/a>.<a href=\"https:\/\/l.facebook.com\/l.php?u=https%3A%2F%2Fbit.ly%2F3vfloEb%3Ffbclid%3DIwAR0nYKAmilriNZURCCikuTvweDlI3Q-tU95xUTEq_9VPrljd57EjoiZIam8&amp;h=AT1TyTow6ZUAoaV5qQ0h82Gu58NtpQfWM1wLrJQAXArk0-QvlPDNQVxbtShBvcvKVI-DZL90Z6sIvRJX8JcxPKTwcGfv59Gz-ZkVxniCvttmmrLQmNj8i5qQyakmWAnBeZ5uysgfKSUeQDGIjYsI&amp;__tn__=-UK-R&amp;c[0]=AT0NEu2r43XJ-0UEbBfVepWqyEZMUzoYz8AwUA5LWrxYJK_Qq12eNNvZ-Q0gnMob4yICH8Cg9BBLMzANeRXn7ValnqfiLAem_Z4NW_4XSWt4VDSrD62MBxuraSS2tkLU85Af1aUIsSm8q23z3O4daVfZwEPwTm_3Ok5rkcHtwM_6LITr6Pd8Co46knBQwPyIgEtIh1jCtOd0f9XY1g\" target=\"_blank\" rel=\"nofollow noopener\" class=\"oajrlxb2 g5ia77u1 qu0x051f esr5mh6w e9989ue4 r7d6kgcz rq0escxv nhd2j8a9 nc684nl6 p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso i1ao9s8h esuyzwwr f1sip0of lzcic4wl py34i1dx gpro0wi8\" tabindex=\"0\" role=\"link\"><\/a><\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6150\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult.png\" alt=\"pollResult\" width=\"650\" height=\"629\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult.png 893w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult-300x291.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult-768x744.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2>What&#8217;s next?<\/h2>\n<p>C++17 makes type deduction more powerful. First, automatic type deduction is possible for non-type template parameters, and second, class templates can also deduce their arguments. In particular, class template argument deduction makes the life of a programmer much more straightforward.<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div id=\"simple-translate\">\n<div>\n<div class=\"simple-translate-button isShow\" style=\"background-image: url('moz-extension:\/\/981aa874-2db4-44d3-a97f-b02a72476831\/icons\/512.png'); height: 22px; width: 22px; top: 351px; left: 884px;\">&nbsp;<\/div>\n<div class=\"simple-translate-panel\" style=\"width: 300px; height: 200px; top: 0px; left: 0px; font-size: 13px; background-color: #ffffff;\">\n<div class=\"simple-translate-result-wrapper\" style=\"overflow: hidden;\">\n<div class=\"simple-translate-move\" draggable=\"draggable\">&nbsp;<\/div>\n<div class=\"simple-translate-result-contents\">\n<p class=\"simple-translate-result\" dir=\"auto\" style=\"color: #000000;\">&nbsp;<\/p>\n<p class=\"simple-translate-candidate\" dir=\"auto\" style=\"color: #737373;\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>It is pretty interesting how the compiler deduces the types for the template arguments. To make it short, you get most of the time the type you expect. The rules do not only apply to function templates (C++98) but also to&nbsp;auto (C++11), to class templates (C++17), and concepts (C++20).<\/p>\n","protected":false},"author":21,"featured_media":6152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6160","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6160","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=6160"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6160\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6152"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}