{"id":6134,"date":"2021-05-04T06:03:03","date_gmt":"2021-05-04T06:03:03","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/function-templates\/"},"modified":"2021-05-04T06:03:03","modified_gmt":"2021-05-04T06:03:03","slug":"function-templates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/function-templates\/","title":{"rendered":"Function Templates"},"content":{"rendered":"<p>A function template is a family of functions. In this post, I want to dive deeper into function templates.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6127\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/templatesNew.png\" alt=\"templatesNew\" width=\"600\" height=\"366\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/templatesNew.png 902w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/templatesNew-300x183.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/templatesNew-768x468.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Here is a short reminder to get you on the same page.<\/p>\n<p>When you instantiate a function template such as <code>max<\/code> for <code>int<\/code><code><\/code> and <code>double<\/code><code><\/code><code><\/code> <code><\/code><\/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\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>);\r\n    max(<span style=\"color: #ff6600;\">10.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p><code><\/code>the compiler generates a fully specialized function template for int and <code>double<\/code>:<code> max<strong>&lt;int&gt;<\/strong><\/code> and <code>max&lt;double&gt;<\/code>. The generic part is, in both cases, empty:<code> template&lt;&gt;<\/code> Thanks to C++ Insights, here are the insights.<\/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=\"400\" height=\"552\" 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: 400px) 100vw, 400px\" \/><\/p>\n<p>Okay, now I can dive into the details. What happens when function templates and non-template functions (in short functions) overload?<\/p>\n<\/p>\n<h2>Overloading of Function Templates and Functions<\/h2>\n<p>Let me use the function <code>max<\/code> once more. This time I instantiate it for <code>float<\/code> and <code>double,<\/code> and I provide a function <code>max<\/code> also taking doubles.<\/p>\n<p>Here is my following 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: #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;\">double<\/span> max(<span style=\"color: #007788; font-weight: bold;\">double<\/span> lhs, <span style=\"color: #007788; font-weight: bold;\">double<\/span> 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.5f<\/span>, <span style=\"color: #ff6600;\">5.5f<\/span>); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    max(<span style=\"color: #ff6600;\">10.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;You may guess my question. What happens in lines (1) and (2)? Here are a few questions?<\/p>\n<ul>\n<li>Line (1): Does the compiler choose the function template or the function and promote the <code>float<\/code> to <code>double<\/code>.<\/li>\n<li>Line (2): Either the function and the function templates are ideal fits. This seems to be ambiguous and may cause a compiler error.<\/li>\n<\/ul>\n<p>The answer to questions is pretty intuitive and follows the general rule in C++. The compiler chooses the best-fitting function.<\/p>\n<ul>\n<li>Line (1): The function template is the better fit because the function would require a promotion from float to double.<\/li>\n<li>Line (2): The function template and the function are ideal fits. In this case, an additional rule kicks in. When both are equally good fits, the compiler prefers the function.<\/li>\n<\/ul>\n<p>As before, <a href=\"https:\/\/cppinsights.io\/lnk?code=dGVtcGxhdGUgPHR5cGVuYW1lIFQ+ClQgbWF4KFQgbGhzLFQgcmhzKSB7CiAgICByZXR1cm4gKGxocyA+IHJocyk\/IGxocyA6IHJoczsKfQoKZG91YmxlIG1heChkb3VibGUgbGhzLCBkb3VibGUgcmhzKSB7CiAgICByZXR1cm4gKGxocyA+IHJocyk\/IGxocyA6IHJoczsKfQoKaW50IG1haW4oKSB7CiAgCiAgICBtYXgoMTAuNWYsIDUuNWYpOwogICAgbWF4KDEwLjUsIDUuNSk7CiAgCn0KCg==&amp;insightsOptions=cpp2a&amp;std=cpp2a&amp;rev=1.0\"> C++ Insights<\/a> helps to visualize this process.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6129\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplateFloatDoubleCircle.png\" alt=\"functionTemplateFloatDouble\" width=\"400\" height=\"512\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplateFloatDoubleCircle.png 457w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplateFloatDoubleCircle-234x300.png 234w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>The screenshot shows it explicitly. Only the use of the function template <code>max<\/code> with <code>float<\/code> (line 2) triggers the instantiations of the function template.<\/p>\n<p>Let&#8217;s go further in our journey through the basics of function templates.<\/p>\n<p>First disclaimer: I ignore <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/concepts\">concepts <\/a>in this post.<\/p>\n<h2>Different Template Arguments<\/h2>\n<p>Let me use my function template <code>max<\/code> with two values having different types.<\/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\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.5f<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;Let&#8217;s try it out on <a href=\"https:\/\/cppinsights.io\/lnk?code=dGVtcGxhdGUgPHR5cGVuYW1lIFQ+ClQgbWF4KFQgbGhzLFQgcmhzKSB7CiAgICByZXR1cm4gKGxocyA+IHJocyk\/IGxocyA6IHJoczsKfQoKaW50IG1haW4oKSB7CiAgCiAgICBtYXgoMTAuNWYsIDUuNSk7CiAgCn0KCg==&amp;insightsOptions=cpp2a&amp;std=cpp2a&amp;rev=1.0\">C++ Insights<\/a>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6130\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplatesFloatDoubleError.png\" alt=\"functionTemplatesFloatDoubleError\" width=\"800\" height=\"105\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplatesFloatDoubleError.png 1220w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplatesFloatDoubleError-300x39.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplatesFloatDoubleError-1024x133.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/functionTemplatesFloatDoubleError-768x100.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>&nbsp;Wow! What is happening here? Why is the <code>float<\/code> not promoted to a <code>double<\/code>? Honestly, the compiler thinks differently, and let me explain how.<\/p>\n<ul>\n<li>The compiler deduces the template argument from the function argument if possible. In this case, it&#8217;s possible.<\/li>\n<li>The compiler does this process of template argument deduction for each function argument.<\/li>\n<li>For <code>10.5f<\/code> the compiler deduces <code>float <\/code>for&nbsp; <code>T,<\/code> for<code> 5.5<\/code> the compiler deduces<code> double<\/code> for <code>T<\/code>.<\/li>\n<li>Of course, T cannot be <code>float<\/code> and <code>double<\/code> at the same time. Because of this ambiguity, the compilation failed.<\/li>\n<\/ul>\n<p>Second disclaimer: I simplified the process of template argument deduction. I will write an additional post about template argument deduction for function templates and class templates in a future post.<\/p>\n<p>Of course, we want to compare values of different types.<\/p>\n<h3>Two Type Parameters<\/h3>\n<p>The solution seems to be straightforward. I introduce a second type parameter.<\/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: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">???<\/span> max(T lhs,T2 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.5f<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>This is easy! Right? But there is a severe problem. Do you see the three question marks as return type? This problem typically occurs when your function template has more than one type parameter. What should be the return type?<\/p>\n<p>In this concrete case, should the return type be T, T2, or a Type R derived from T and T2? This task was challenging before C++11, but it is pretty easy with C++11.<\/p>\n<p>Here are a few solutions I have in mind:<\/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;\">\/\/ automaticReturnTypeDeduction.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;type_traits&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> T1, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">typename<\/span> std<span style=\"color: #555555;\">::<\/span>conditional<span style=\"color: #555555;\">&lt;<\/span>(<span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(T1) <span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(T2)), T1, T2<span style=\"color: #555555;\">&gt;::<\/span>type max1(T1 lhs,T2 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: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T1, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">typename<\/span> std<span style=\"color: #555555;\">::<\/span>common_type<span style=\"color: #555555;\">&lt;<\/span>T1, T2<span style=\"color: #555555;\">&gt;::<\/span>type max2(T1 lhs,T2 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: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T1, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> max3(T1 lhs,T2 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    max1(<span style=\"color: #ff6600;\">10.5f<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);                  \r\n    max2(<span style=\"color: #ff6600;\">10.5f<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);                  \r\n    max3(<span style=\"color: #ff6600;\">10.5f<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>);                  \r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;The first two versions <code>max1<\/code> (line 1) and <code>max2<\/code> (line 2) are based on the type-traits library. The third version <code>max3<\/code> (line 3) uses the automatic type deduction of <code>auto<\/code>.<\/p>\n<ul>\n<li><code>max1<\/code> (line 1): <code>typename<\/code> s<code>td::conditional&lt;(sizeof(T1) &gt; sizeof(T2)), T1, T2&gt;::type<\/code> returns the type<code> T1<\/code>, or<code> T2<\/code> that is bigger. <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/conditional\">std::conditional<\/a> is a kind of compile-time ternary operator.<\/li>\n<li><code>max2<\/code> (line2):<code> typename td::common_type&lt;T1, T2&gt;::type<\/code> returns the common type of the types T1 and T2.<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/common_type\"> std::common_type<\/a> can accept an arbitrary number of arguments.<\/li>\n<li><code>max3<\/code> (line3): <code>auto<\/code> should be self-explanatory.<\/li>\n<\/ul>\n<div>Maybe you are irritated by the <code>typename<\/code> in-front of the return type of the function template <code>max1<\/code> and max2. T1 and T2 are dependent names. What is a dependent name? A dependent name is essentially a name that depends on a template parameter. In this case, we have to give the compiler a hint that T1 and T2 are types. Essentially, they can also be non-types or templates.<\/div>\n<div>&nbsp;<\/div>\n<div>Third disclaimer: I write in an additional post about dependent types.<\/div>\n<div>&nbsp;<\/div>\n<div>Let&#8217;s see what C++ Insights provides. I only show the template instantiations. If you want to analyze the entire program, follow this link: <a href=\"https:\/\/cppinsights.io\/lnk?code=Ly8gYXV0b21hdGljUmV0dXJuVHlwZURlZHVjdGlvbi5jcHAKCiNpbmNsdWRlIDx0eXBlX3RyYWl0cz4KCnRlbXBsYXRlIDx0eXBlbmFtZSBUMSwgdHlwZW5hbWUgVDI+ICAgICAgLy8gKDEpCnR5cGVuYW1lIHN0ZDo6Y29uZGl0aW9uYWw8KHNpemVvZihUMSkgPiBzaXplb2YoVDIpKSwgVDEsIFQyPjo6dHlwZSBtYXgxKFQxIGxocyxUMiByaHMpIHsKICAgIHJldHVybiAobGhzID4gcmhzKT8gbGhzIDogcmhzOwp9Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgVDEsIHR5cGVuYW1lIFQyPiAgICAgIC8vICgyKQp0eXBlbmFtZSBzdGQ6OmNvbW1vbl90eXBlPFQxLCBUMj46OnR5cGUgbWF4MihUMSBsaHMsVDIgcmhzKSB7CiAgICByZXR1cm4gKGxocyA+IHJocyk\/IGxocyA6IHJoczsKfQoKdGVtcGxhdGUgPHR5cGVuYW1lIFQxLCB0eXBlbmFtZSBUMj4gICAgIC8vICgzKQphdXRvIG1heDMoVDEgbGhzLFQyIHJocykgewogICAgcmV0dXJuIChsaHMgPiByaHMpPyBsaHMgOiByaHM7Cn0KCmludCBtYWluKCkgewogIAogIAltYXgxKDEwLjVmLCA1LjUpOyAgICAgICAgICAgICAgICAgIAogICAgbWF4MigxMC41ZiwgNS41KTsgICAgICAgICAgICAgICAgICAKICAgIG1heDMoMTAuNWYsIDUuNSk7ICAgICAgICAgICAgICAgICAgCiAgCn0=&amp;insightsOptions=cpp2a&amp;std=cpp2a&amp;rev=1.0\">C++ Insights<\/a>.<\/div>\n<div>&nbsp;<\/div>\n<ul>\n<li><code>max1<\/code>(line 1): You can only guess the return type. In the return statement, the smaller type (float) is converted to <code>double<\/code>. <img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6131\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max1.png\" alt=\"max1\" width=\"650\" height=\"87\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max1.png 1065w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max1-300x40.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max1-1024x137.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max1-768x102.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/li>\n<li><code>max2<\/code>(line 2): As for <code>max1<\/code>, the return statement gives an idea about the return type: the float value is converted to <code>double<\/code>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6132\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max2.png\" alt=\"max2\" width=\"500\" height=\"94\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max2.png 778w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max2-300x57.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max2-768x145.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<ul>\n<li><code>max3<\/code> (line 3): Now, we can see the return type explicitly. It is a <code>double<\/code>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6133\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max3.png\" alt=\"max3\" width=\"500\" height=\"85\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max3.png 838w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max3-300x51.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/05\/max3-768x131.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<div>&nbsp;<\/div>\n<h2>What&#8217;s next?<\/h2>\n<p>In this installment, I solved the challenge of different types of function arguments by using more than one type parameter. <a href=\"https:\/\/www.modernescpp.com\/index.php\/function-templates-more-details\">Next time<\/a>, I will take a different approach and explicitly specify the template arguments.&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A function template is a family of functions. In this post, I want to dive deeper into function templates.<\/p>\n","protected":false},"author":21,"featured_media":6127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6134","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\/6134","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=6134"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6134\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6127"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}