{"id":6173,"date":"2021-07-09T07:07:17","date_gmt":"2021-07-09T07:07:17","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/full-specialization-of-function-templates\/"},"modified":"2021-07-09T07:07:17","modified_gmt":"2021-07-09T07:07:17","slug":"full-specialization-of-function-templates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/full-specialization-of-function-templates\/","title":{"rendered":"Full Specialization of Function Templates"},"content":{"rendered":"<p>As you may know from my previous post, <a href=\"https:\/\/www.modernescpp.com\/index.php\/template-specialization\">Template Specialization<\/a>, a function template can only be full but not partially specialized. To make my long story short:<a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-specialize-function\"> Don&#8217;t specialize function templates<\/a>. Just use function overloading.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6165\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization.png\" alt=\"TemplateSpecialization\" width=\"650\" height=\"409\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization.png 926w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization-300x189.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization-768x483.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>You may wonder why I write about a feature of C++ you should not use. The reason is quite simple. When you see the surprising behavior of fully specialized function templates, you will hopefully use a non-generic function instead.<\/p>\n<h2>Don&#8217;t Specialize Function Templates<\/h2>\n<p>Maybe the title reminds you? Right. This title is from the C++ Core Guidelines: <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-specialize-function\">T.144: Don\u2019t specialize function templates<\/a><\/p>\n<p>The reason for the rules is relatively short: function template specialization doesn&#8217;t participate in overloading. Let&#8217;s see what that means. My program is based on the program snippet from Dimov\/<a href=\"https:\/\/en.wikipedia.org\/wiki\/David_Abrahams_(computer_programmer)\">Abrahams<\/a>.<\/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;\">\/\/ dimovAbrahams.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ getTypeName<\/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<span style=\"color: #555555;\">&gt;<\/span>            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1) primary template<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName(T){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"unknown\"<\/span>;\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> T<span style=\"color: #555555;\">&gt;<\/span>            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) primary template that overloads (1)<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName(T<span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"pointer\"<\/span>;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3) explicit specialization of (2)<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"int pointer\"<\/span>;\r\n}\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ getTypeName2<\/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<span style=\"color: #555555;\">&gt;<\/span>            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4) primary template<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName2(T){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"unknown\"<\/span>;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5) explicit specialization of (4)<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName2(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"int pointer\"<\/span>;\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> T<span style=\"color: #555555;\">&gt;<\/span>            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6) primary template that overloads (4)<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName2(T<span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"pointer\"<\/span>;\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> '\\n'<span style=\"color: #555555;\"><\/span>;\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">* <\/span>p;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getTypeName(p): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getTypeName(p) <span style=\"color: #555555;\">&lt;&lt;<\/span>  '\\n';  \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getTypeName2(p): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getTypeName2(p) <span style=\"color: #555555;\">&lt;&lt;<\/span>  '\\n';\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  '\\n';\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Admittedly, the code looks quite boring but bear with me. I defined inline (1) the primary template&nbsp;<span style=\"font-family: courier new, courier;\">getTypeName.<\/span> (2) is an overload for pointers, and (3) a full specialization for an <code>int<\/code> pointer. In the case of <span style=\"font-family: courier new, courier;\">getTypeName2,<\/span> I made a small variation. I put the explicit <code>specialisation<\/code> (5) before the overload for pointers (6).<\/p>\n<p>This reordering has surprising consequences.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5632\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/dimovAbrahams.png\" alt=\"dimovAbrahams\" width=\"356\" height=\"217\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/dimovAbrahams.png 356w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/dimovAbrahams-300x183.png 300w\" sizes=\"auto, (max-width: 356px) 100vw, 356px\" \/><\/p>\n<p>In the first case, the full specialization for the <span style=\"font-family: courier new, courier;\">int<\/span> pointer is called, and in the second case, the overload of pointers. What?&nbsp; This non-intuitive behavior is because overload resolution ignores function template specialization. Overload resolution operates on primary templates and functions. In both cases, overload resolutions found both primary templates. In the first case (<span style=\"font-family: courier new, courier;\">getTypeName<\/span>), the pointer variant is the better fit and, therefore, the explicit specialization for the <span style=\"font-family: courier new, courier;\">int<\/span> pointer was chosen. The pointer variant was chosen in the second variant (getTypeName2), but the full specialization belongs to the primary template (line 4). Consequently, it was ignored.<\/p>\n<p>I know this wasn&#8217;t very easy. Just keep the rule in mind:<strong> Don&#8217;t specialize function templates; use non-generic functions instead.<\/strong><\/p>\n<p>Do you want to have proof of my statement? Here it is: Making out of the explicit specialization in (3) and (5) non-generic functions solves the issue. I have to comment out the template declaration<code> template&lt;&gt;<\/code>. For simplicity reasons, I removed the other comments.<\/p>\n<p>&nbsp;<\/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;\">\/\/ dimovAbrahams.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ getTypeName<\/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<span style=\"color: #555555;\">&gt;<\/span>           \r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName(T){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"unknown\"<\/span>;\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> T<span style=\"color: #555555;\">&gt;<\/span>            \r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName(T<span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"pointer\"<\/span>;\r\n}\r\n<br \/>\/\/ <span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"int pointer\"<\/span>;\r\n}\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ getTypeName2<\/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<span style=\"color: #555555;\">&gt;<\/span>           \r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName2(T){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"unknown\"<\/span>;\r\n}\r\n<br \/>\/\/ <span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;&gt;<\/span>                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5) <\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName2(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"int pointer\"<\/span>;\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> T<span style=\"color: #555555;\">&gt;<\/span>           \r\nstd<span style=\"color: #555555;\">::<\/span>string getTypeName2(T<span style=\"color: #555555;\">*<\/span>){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"pointer\"<\/span>;\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> '\\n'<span style=\"color: #555555;\"><\/span>;\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">* <\/span>p;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getTypeName(p): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getTypeName(p) <span style=\"color: #555555;\">&lt;&lt;<\/span>  '\\n';  \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getTypeName2(p): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getTypeName2(p) <span style=\"color: #555555;\">&lt;&lt;<\/span>  '\\n';\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  '\\n';\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, function overloading works as expected, and the non-generic function takes an <code>int<\/code> pointer is used.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6171\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/dimovAbrahams2.png\" alt=\"dimovAbrahams2\" width=\"380\" height=\"232\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/dimovAbrahams2.png 380w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/dimovAbrahams2-300x183.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/p>\n<p>I already wrote about Template Arguments. But I forgot one crucial fact. You can provide default template arguments for function templates and class templates.<\/p>\n<\/p>\n<h2>Default Template Arguments<\/h2>\n<p>What is common to the Standard Template Library class templates (STL)? Yes! Many of the template arguments have defaults.<\/p>\n<p>Here are a few examples.<\/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>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Allocator <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">vector<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Key,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Hash <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>hash<span style=\"color: #555555;\">&lt;<\/span>Key<span style=\"color: #555555;\">&gt;<\/span>,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> KeyEqual <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>equal_to<span style=\"color: #555555;\">&lt;<\/span>Key<span style=\"color: #555555;\">&gt;<\/span>,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Allocator <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>pair<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> Key, T<span style=\"color: #555555;\">&gt;&gt;<\/span>\r\n<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">unordered_map<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Allocator <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">deque<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Container <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>deque<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">stack<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> CharT,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Traits <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>char_traits<span style=\"color: #555555;\">&lt;<\/span>CharT<span style=\"color: #555555;\">&gt;<\/span>,\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Allocator <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>CharT<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">basic_string<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This is part of the power of the STL:<\/p>\n<ul>\n<li>Each container has a default allocator that depends on its elements.<\/li>\n<li>You have to specify the required arguments, such as the essential type and value type, for a <code>std::unordered_map: std::unordered_map&lt;std::string, int&gt;. <br \/><\/code><\/li>\n<li>\n<p>You can instantiate a<code> std::unordered_map<\/code> using a particular hash function returning the has value for the key and a unique binary predicate determining if two keys are equal: <code>std::unordered_map&lt;std::string, int, MyHash&gt;<\/code>, or <code>std::unordered_map&lt;std::string, int, MyHash, MyBinaryPredicate&gt;<\/code>.<\/p>\n<\/li>\n<li>std::string is just an alias for common character types. Here are the aliases based on<code> std::basic_string.<\/code><\/li>\n<\/ul>\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%; padding-left: 30px;\">std<span style=\"color: #555555;\">::<\/span>string         std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>wstring \t    std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">wchar_t<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>u8string \t    std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char8_t<\/span><span style=\"color: #555555;\">&gt;<\/span>  (C<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">20<\/span>)\r\nstd<span style=\"color: #555555;\">::<\/span>u16string      std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char16_t<\/span><span style=\"color: #555555;\">&gt;<\/span> (C<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">11<\/span>)\r\nstd<span style=\"color: #555555;\">::<\/span>u32string      std<span style=\"color: #555555;\">::<\/span>basic_string<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char32_t<\/span><span style=\"color: #555555;\">&gt;<\/span> (C<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">11<\/span>)\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Of course, when a template argument has a default, the following template arguments must also have a default.<\/p>\n<p>So far, I only wrote about default template arguments for class templates. I want to end this post with an example of function templates.<\/p>\n<p>Assume I want to decide for a few objects having the same type which one is smaller. An algorithm such as <code>isSmaller<\/code> models a generic idea and should, therefore, be a template.<\/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;\">\/\/ templateDefaultArguments.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Account<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Account(<span style=\"color: #007788; font-weight: bold;\">double<\/span> b)<span style=\"color: #555555;\">:<\/span> balance(b){}\r\n  <span style=\"color: #007788; font-weight: bold;\">double<\/span> getBalance() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> balance;\r\n  }\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n  <span style=\"color: #007788; font-weight: bold;\">double<\/span> balance;\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> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Pred <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>less<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&gt;<\/span>                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> isSmaller(T fir, T sec, Pred pred <span style=\"color: #555555;\">=<\/span> Pred() ){\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> pred(fir,sec);\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> std<span style=\"color: #555555;\">::<\/span>boolalpha <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;\">\"isSmaller(3,4): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> isSmaller(<span style=\"color: #ff6600;\">3<\/span>,<span style=\"color: #ff6600;\">4<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) <\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller(2.14,3.14): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> isSmaller(<span style=\"color: #ff6600;\">2.14<\/span>,<span style=\"color: #ff6600;\">3.14<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller(std::string(abc),std::string(def)): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> \r\n                isSmaller(std<span style=\"color: #555555;\">::<\/span>string(<span style=\"color: #cc3300;\">\"abc\"<\/span>),std<span style=\"color: #555555;\">::<\/span>string(<span style=\"color: #cc3300;\">\"def\"<\/span>)) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #007788; font-weight: bold;\">bool<\/span> resAcc<span style=\"color: #555555;\">=<\/span> isSmaller(Account(<span style=\"color: #ff6600;\">100.0<\/span>),Account(<span style=\"color: #ff6600;\">200.0<\/span>),                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n               [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> Account<span style=\"color: #555555;\">&amp;<\/span> fir, <span style=\"color: #006699; font-weight: bold;\">const<\/span> Account<span style=\"color: #555555;\">&amp;<\/span> sec){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir.getBalance() <span style=\"color: #555555;\">&lt;<\/span> sec.getBalance(); });\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller(Account(100.0),Account(200.0)): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> resAcc <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #007788; font-weight: bold;\">bool<\/span> acc<span style=\"color: #555555;\">=<\/span> isSmaller(std<span style=\"color: #555555;\">::<\/span>string(<span style=\"color: #cc3300;\">\"3.14\"<\/span>),std<span style=\"color: #555555;\">::<\/span>string(<span style=\"color: #cc3300;\">\"2.14\"<\/span>),              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n            [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> fir, <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> sec){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>stod(fir) <span style=\"color: #555555;\">&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>stod(sec); });\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"isSmaller(std::string(3.14),std::string(2.14)): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> acc <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>In the default case (2), <code>isSmaller<\/code> works as expected. <code>isSmaller<\/code> (1) uses the template argument <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/less\"><code>std::less<\/code><\/a> , one of many <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\">predefined function objects<\/a> in the STL. It applies the less-than operator <code>&lt;<\/code> to its arguments. To use it, I had to instantiate std::less&lt;T&gt; in the following line:<code> Pred pred = Pred()<\/code>.<\/p>\n<p>I can compare accounts (3) or strings (4) thanks to the default template argument.&nbsp;<code>Account<\/code> does not support the less-than-operator. Nevertheless, I can compare <code>Account<\/code>s. (3). Additionally, I want to compare strings not lexicographically but based on their internal number (4). Providing the two lambda expressions in (3) and (4) as binary predicates let me do my job successfully.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6172\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templateDefaultArgument.png\" alt=\"templateDefaultArgument\" width=\"600\" height=\"277\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templateDefaultArgument.png 626w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/07\/templateDefaultArgument-300x138.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>When you study the graphic at the beginning of this post, you see that I&#8217;m done with the basics of templates. In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/parallel-algorithms-of-the-stl-with-gcc\">next post<\/a> about templates, I will dive further into the details and write about template instantiation.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you may know from my previous post, Template Specialization, a function template can only be full but not partially specialized. To make my long story short: Don&#8217;t specialize function templates. Just use function overloading.<\/p>\n","protected":false},"author":21,"featured_media":6165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6173","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\/6173","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=6173"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6165"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}