{"id":5629,"date":"2019-02-08T07:40:08","date_gmt":"2019-02-08T07:40:08","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-other-template-rules\/"},"modified":"2019-02-08T07:40:08","modified_gmt":"2019-02-08T07:40:08","slug":"c-core-guidelines-other-template-rules","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-other-template-rules\/","title":{"rendered":"C++ Core Guidelines: Other Template Rules"},"content":{"rendered":"<p>Today, I write about the few remaining rules to templates. Because a collective name is missing, they put the heterogeneous rules to templates in the section other. The rules are about best practices but also about surprises.&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5626\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/maki-3295891_1280.jpg\" alt=\"maki 3295891 1280\" width=\"600\" height=\"383\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/maki-3295891_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/maki-3295891_1280-300x191.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/maki-3295891_1280-1024x654.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/maki-3295891_1280-768x490.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Here are the rules for this post.<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-name\">T.140: Name all operations with potential for reuse<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-lambda\">T.141: Use an unnamed lambda if you need a simple function object in one place only<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-nongeneric\">T.143: Don\u2019t write unintentionally nongeneric code<\/a><\/li>\n<\/ul>\n<p>&nbsp;The first rule is about best practices.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-name\">T.140: Name all operations with potential for reuse<\/a><\/h2>\n<p>Honestly, I&#8217;m not so sure why this rule belongs to templates. Maybe templates are about reuse, or the example in the guidelines uses the <span style=\"font-family: courier new, courier;\">std::find_if<\/span> algorithm of the Standard Template Library. Anyway, the rule is fundamental from the code quality perspective.<\/p>\n<p>Imagine you have a vector of records. Each record consists of a name, an address, and an id. You often want to find a record with a specific name; but to make it more challenging, you ignore the case sensitivity of the names.<\/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;\">\/\/ records.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;cctype&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<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Rec {                                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>string name;\r\n    std<span style=\"color: #555555;\">::<\/span>string addr;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> id;         \r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>Rec<span style=\"color: #555555;\">&gt;<\/span> vr{ {<span style=\"color: #cc3300;\">\"Grimm\"<\/span>, <span style=\"color: #cc3300;\">\"Munich\"<\/span>, <span style=\"color: #ff6600;\">1<\/span>},                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n                         {<span style=\"color: #cc3300;\">\"huber\"<\/span>, <span style=\"color: #cc3300;\">\"Stuttgart\"<\/span>, <span style=\"color: #ff6600;\">2<\/span>},\r\n                         {<span style=\"color: #cc3300;\">\"Smith\"<\/span>, <span style=\"color: #cc3300;\">\"Rottenburg\"<\/span>, <span style=\"color: #ff6600;\">3<\/span>},\r\n                         {<span style=\"color: #cc3300;\">\"black\"<\/span>, <span style=\"color: #cc3300;\">\"Hanover\"<\/span>, <span style=\"color: #ff6600;\">4<\/span>} };\r\n                         \r\n    std<span style=\"color: #555555;\">::<\/span>string name <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"smith\"<\/span>;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> rec <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>find_if(vr.begin(), vr.end(), [<span style=\"color: #555555;\">&amp;<\/span>](Rec<span style=\"color: #555555;\">&amp;<\/span> r) {  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (r.name.size() <span style=\"color: #555555;\">!=<\/span> name.size()) <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;            \r\n        <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> r.name.size(); <span style=\"color: #555555;\">++<\/span>i){                   \r\n            <span style=\"color: #006699; font-weight: bold;\">if<\/span> (std<span style=\"color: #555555;\">::<\/span>tolower(r.name[i]) <span style=\"color: #555555;\">!=<\/span> std<span style=\"color: #555555;\">::<\/span>tolower(name[i])) <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n        }\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">true<\/span>;                                               \r\n    });\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (rec <span style=\"color: #555555;\">!=<\/span> vr.end()){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> rec<span style=\"color: #555555;\">-&gt;<\/span>name <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> rec<span style=\"color: #555555;\">-&gt;<\/span>addr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> rec<span style=\"color: #555555;\">-&gt;<\/span>id <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The struct <span style=\"font-family: courier new, courier;\">Rec<\/span> (line 1) has only public members; therefore, I can use <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/aggregate_initialization\">aggregate initialization<\/a> and initialize all members directly in line (2). In line (3), I use a lambda function to search for the &#8221; smith &#8221; record<span style=\"font-family: courier new, courier;\">.<\/span> First, I check if both names have the same size and second if the characters are identical when compared to case-insensitive. &nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5627\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/records.png\" alt=\"records\" width=\"250\" height=\"160\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/records.png 319w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/records-300x192.png 300w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>What&#8217;s the problem with the code? The requirement of the case-insensitive comparison of strings is too common, and we should put the solution in an object, give it a name and reuse it.<\/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: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #cc00ff;\">compare_insensitive<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> a, <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> b)    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.size() <span style=\"color: #555555;\">!=<\/span> b.size()) <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> a.size(); <span style=\"color: #555555;\">++<\/span>i){\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (std<span style=\"color: #555555;\">::<\/span>tolower(a[i]) <span style=\"color: #555555;\">!=<\/span> std<span style=\"color: #555555;\">::<\/span>tolower(b[i])) <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">true<\/span>;\r\n}\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>string name <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"smith\"<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>find_if(vr.begin(), vr.end(),         \r\n    [<span style=\"color: #555555;\">&amp;<\/span>](Rec<span style=\"color: #555555;\">&amp;<\/span> r) { compare_insensitive(r.name, name); }\r\n);\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> vs{<span style=\"color: #cc3300;\">\"Grimm\"<\/span>, <span style=\"color: #cc3300;\">\"huber\"<\/span>, <span style=\"color: #cc3300;\">\"Smith\"<\/span>, <span style=\"color: #cc3300;\">\"black\"<\/span>};        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res2 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>find_if(vs.begin(), vs.end(),\r\n    [<span style=\"color: #555555;\">&amp;<\/span>](std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> r) { compare_insensitive(r, name); }\r\n);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function <span style=\"font-family: courier new, courier;\">compare_insensitive<\/span> (line 1) gives a general concept a name. Now,&nbsp; I can use it for a vector of strings (line 2).<\/p>\n<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-lambda\">T.141: Use an unnamed lambda if you need a simple function object in one place only<\/a><\/h2>\n<p>I often discuss this in my classes: When should I use a function (function object) or a lambda function? Honestly, I have no easy answer. Here, two meta-rules of code quality contradict:<\/p>\n<ol>\n<li>Don&#8217;t repeat yourself. (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Don%27t_repeat_yourself\">DRY<\/a>)<\/li>\n<li>Explicit is better than implicit. (<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0020\/\">The Zen of Python<\/a>)<\/li>\n<\/ol>\n<p>Sorry, I borrowed the second point from Python. But what does that mean? Imagine you have an old-fashioned Fortran programmer in your team, and he says to you: &#8220;Each name must have three characters.&#8221; So, you end with the following code.<\/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;\">auto<\/span> eUE <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>remove_if(use.begin(), use.end(), igh);           \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>What does the name <span style=\"font-family: courier new, courier;\">igh<\/span> stand for? igh stands for a id greater hundred. Now, you are forced to document the usage of the predicate.<\/p>\n<p>But If you use a lambda function, the code documents itself.<\/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;\">auto<\/span> earlyUsersEnd <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>remove_if(users.begin(), users.end(),\r\n                                    [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> User <span style=\"color: #555555;\">&amp;user<\/span>) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> user.id <span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #ff6600;\">100<\/span>; });                    \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I had discussions with Fortran programmers about names. Admittedly, more arguments, such as code locality versus code size, speak for or against lambda functions, but &#8220;Don&#8217;t repeat yourself&#8221; versus &#8220;Explicit is better than implicit&#8221; are my key arguments.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-nongeneric\">T.143: Don\u2019t write unintentionally nongeneric code<\/a><\/h2>\n<p>A short example says more than a long explanation. In the following example, I iterate through a <span style=\"font-family: courier new, courier;\">std::vector<\/span>, a <span style=\"font-family: courier new, courier;\">std::deque<\/span>, and a <span style=\"font-family: courier new, courier;\">std::list<\/span>.<\/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;\">\/\/ notGeneric.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;deque&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;list&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&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> Cont<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> justIterate(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Cont<span style=\"color: #555555;\">&amp;<\/span> cont){\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> itEnd <span style=\"color: #555555;\">=<\/span> cont.end();\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> it <span style=\"color: #555555;\">=<\/span> cont.begin(); it <span style=\"color: #555555;\">&lt;<\/span> itEnd; <span style=\"color: #555555;\">++<\/span>it) {    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #0099ff; font-style: italic;\">\/\/ do something<\/span>\r\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>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> vecInt{<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>};\r\n    justIterate(vecInt);                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>deque<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> deqInt{<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>};\r\n    justIterate(deqInt);                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>list<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> listInt{<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>};\r\n    justIterate(listInt);                               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\n}                   \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The code looks innocent, but the compilation breaks when I want to compile the program. I get about 100 lines of error messages.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5628\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/notGeneric.png\" alt=\"notGeneric\" width=\"550\" height=\"163\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/notGeneric.png 967w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/notGeneric-300x89.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/notGeneric-768x227.png 768w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>At the beginning of the error message, you see it is quite precise: &#8220;<span style=\"font-family: courier new, courier;\">notGeneric.cpp:10:37: error: no match for &#8216;operator&lt;&#8216; (operand types are &#8216;std::_List_const_iterator<\/span>&#8220;.<\/p>\n<p>What is the issue? The issue is in line (1). The iterator comparison (&lt;) works for the <span style=\"font-family: courier new, courier;\">std::vector<\/span> (line 2) and the <span style=\"font-family: courier new, courier;\">std::deque<\/span> (line 3) but breaks for the <span style=\"font-family: courier new, courier;\">std::list<\/span> (line 4). Each container returns an iterator representing its structure. This is in the case of a <span style=\"font-family: courier new, courier;\">std::vector<\/span> and a <span style=\"font-family: courier new, courier;\">std::deque<\/span>, a random access iterator, and in the case of the <span style=\"font-family: courier new, courier;\">std::list<\/span>, a bidirectional iterator. A look at the iterator categories helps a lot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5526\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/IteratorCategories.png\" alt=\"IteratorCategories\" width=\"600\" height=\"362\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/IteratorCategories.png 990w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/IteratorCategories-300x181.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/10\/IteratorCategories-768x464.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>The random access iterator category is a superset of the bidirectional iterator category, and the bidirectional iterator category is a superset of the forward iterator category. Now, the issue is obvious. An iterator given by a list does not support the smaller comparison. Fixing the bug is relatively easy. Iterators of each iterator category support the != comparison. Here is the fixed <span style=\"font-family: courier new, courier;\">justIterate<\/span> function template.<\/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: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Cont<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> justIterate(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Cont<span style=\"color: #555555;\">&amp;<\/span> cont){\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> itEnd <span style=\"color: #555555;\">=<\/span> cont.end();\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> it <span style=\"color: #555555;\">=<\/span> cont.begin(); it <span style=\"color: #555555;\">!=<\/span> itEnd; <span style=\"color: #555555;\">++<\/span>it) {   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #0099ff; font-style: italic;\">\/\/ do something<\/span>\r\n    }\r\n}           \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>By the way, it is typically a bad idea to loop through a container such as I do in the function <span style=\"font-family: courier new, courier;\">justIterate.<\/span> This is a job for an appropriate algorithm of the standard template library.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>My original plan was to write today about rule <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-specialize-function\">T.144: Don\u2019t specialize function templates.<\/a> This rule holds a significant surprise potential. You will see what I mean in the<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-other-template-rules\"> next post.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I write about the few remaining rules to templates. Because a collective name is missing, they put the heterogeneous rules to templates in the section other. The rules are about best practices but also about surprises.&nbsp;<\/p>\n","protected":false},"author":21,"featured_media":5626,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5629","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5629","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=5629"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5629\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5626"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}