{"id":5846,"date":"2020-02-07T08:09:54","date_gmt":"2020-02-07T08:09:54","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-20-concepts-an-evolution-or-a-revolution-in-c\/"},"modified":"2023-06-26T09:55:23","modified_gmt":"2023-06-26T09:55:23","slug":"c-20-concepts-an-evolution-or-a-revolution-in-c","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-20-concepts-an-evolution-or-a-revolution-in-c\/","title":{"rendered":"Concepts in C++20: An Evolution or a Revolution?"},"content":{"rendered":"<p>Let me conclude my series to concepts with this meta-post. Are concepts an evolution or a revolution in C++? The answer to this question bothered me quite a time.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5808\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts.png\" alt=\"TimelineCpp20Concepts\" width=\"650\" height=\"255\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts.png 954w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts-300x118.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts-768x301.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I assume we all know what evolution or revolution means, but let me be more precise. These definitions are quite concise:<\/p>\n<ul>\n<li><strong>Evolution<\/strong>&nbsp;is defined as gradual change, adaptation, progression, and metamorphosis.<\/li>\n<li><strong>Revolution&nbsp;<\/strong>is the forcible overthrow of an entirely new system\u2026drastic, disruptive, far-reaching, meaningful change.<\/li>\n<\/ul>\n<p>To make it short. The crucial difference between evolution and revolution is if the change is gradual (evolution) or disruptive (revolution).<\/p>\n<p>I had many discussions in the series about concepts. Consequentially, I was curious about what your opinion of concepts is. The answers I got are in German. For your convenience, I paraphrase it in English. Interestingly, my readers have a clear tendency to evolve. Honestly, I&#8217;m more on the revolution side.<\/p>\n<p>Which argument speaks for evolution, and which argument speaks for revolution?<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Evolution\"><\/span>Evolution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5842\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/evolution-4107273_1280.jpg\" alt=\"evolution 4107273 1280\" width=\"400\" height=\"276\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/evolution-4107273_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/evolution-4107273_1280-300x207.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/evolution-4107273_1280-1024x706.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/evolution-4107273_1280-768x530.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Image by <a href=\"https:\/\/pixabay.com\/users\/Alexas_Fotos-686414\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=4107273\">Alexas_Fotos<\/a> from <a href=\"https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=4107273\">Pixabay<\/a><\/p>\n<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Clear_Abstraction\"><\/span>Clear Abstraction&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>When appropriately used, concepts should promote clean working with generic code at a higher level of abstraction.<\/p>\n<p>In the longer term, the standard concepts should become increasingly idiomatic in C++. The interoperability and modular work, especially in larger teams, can be made more robust and less prone to errors if more abstract properties of the parameter classes are checked and less for purely syntactic &#8220;rollout&#8221; in generic code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Simple_Definition_and_Meaningful_Error_Messages\"><\/span>Simple Definition and Meaningful Error Messages<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You cannot do anything that you would not have been able to do with <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\">type-traits library<\/a>, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/sfinae\">SFINAE<\/a>, and <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/static_assert\">static_assert<\/a> so far, even if it was very cumbersome and time-consuming. Their advantages lie in the simple definition and meaningful error messages.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Unconstrained_Placeholders\"><\/span>Unconstrained Placeholders<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Since C++11, we have auto to deduce the type of a variable from its initializer.<\/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;\">auto<\/span> integ <span style=\"color: #555555;\">=<\/span> add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">11<\/span>);\r\n\r\nstd<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> myVec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v<span style=\"color: #555555;\">:<\/span> myVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n<\/pre>\n<\/div>\n<p><span style=\"color: #000000; font-family: DejaVuSans, 'DejaVu Sans', arial, sans-serif; font-size: 12.8px; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff; float: none;\"><\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier;\">auto<\/span> is a kind of unconstrained placeholder. With C++20, we can use concepts as constrained placeholders.<\/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: #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\nconcept Integral <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>is_integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value;\r\n\r\nIntegral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> integ <span style=\"color: #555555;\">=<\/span> add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">11<\/span>);\r\n\r\nstd<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> myVec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (Integral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> v<span style=\"color: #555555;\">:<\/span> myVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>To make it concise and evolutionary: you can use a constrained placeholder (concept) in each place you could use an unconstrained placeholder (auto).<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Generic_Lambdas\"><\/span>Generic Lambdas<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;With C++14, you can use a generic lambda (<span style=\"font-family: 'courier new', courier;\">addLambda<\/span>), which essentially becomes a function template (addTemplate).<\/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;\">\/\/ addLambdaGeneric.pp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> addLambda <span style=\"color: #555555;\">=<\/span> [](<span style=\"color: #006699; font-weight: bold;\">auto<\/span> fir, <span style=\"color: #006699; font-weight: bold;\">auto<\/span> sec){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec; }; \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> T2<span style=\"color: #555555;\">&gt;<\/span>                            \r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> addTemplate(T fir, T2 sec){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> 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> addLambda(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">11.5<\/span>);    <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2011.5<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> addTemplate(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">11.5<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2011.5<\/span>\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Using <span style=\"font-family: 'courier new', courier;\">auto<\/span> in a function declaration was not possible with C++14. Since C++20, you can use a constrained placeholder (concept) or an unconstrained placeholder (<span style=\"font-family: 'courier new', courier;\">auto<\/span>) in a function declaration, and the function declaration becomes a function template with unconstrained (<span style=\"font-family: 'courier new', courier;\">auto<\/span>) or constrained (concept) parameters.<\/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;\">\/\/ addUnconstrainedConstrained.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;concepts&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">addUnconstrained<\/span>(<span style=\"color: #006699; font-weight: bold;\">auto<\/span> fir, <span style=\"color: #006699; font-weight: bold;\">auto<\/span> sec){\r\n     <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\r\n}\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>floating_point <span style=\"color: #006699; font-weight: bold;\">auto<\/span> addConstrained(std<span style=\"color: #555555;\">::<\/span>integral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> fir, \r\n                                        std<span style=\"color: #555555;\">::<\/span>floating_point <span style=\"color: #006699; font-weight: bold;\">auto<\/span> sec){\r\n     <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> 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> addUnconstrained(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">11.5<\/span>); <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2011.5<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> addConstrained(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">11.5<\/span>);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2011.5<\/span>\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>To make my point, I intentionally used a strange signature for my addConstrained function.&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Revolution\"><\/span>Revolution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5843\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/france-63022_1280.jpg\" alt=\"france 63022 1280\" width=\"400\" height=\"317\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/france-63022_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/france-63022_1280-300x237.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/france-63022_1280-1024x810.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/france-63022_1280-768x608.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Image by <a href=\"https:\/\/pixabay.com\/users\/WikiImages-1897\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=63022\">WikiImages<\/a> from <a href=\"https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=63022\">Pixabay<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Template_Requirements_are_verified\"><\/span>Template Requirements are verified<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Admittedly, you can specify template requirements in C++11 in the template declaration.<\/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;\">\/\/ requirementsCheckSFINAE.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> T,\r\n         <span style=\"color: #006699; font-weight: bold;\">typename<\/span> std<span style=\"color: #555555;\">::<\/span>enable_if<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>is_integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value, T<span style=\"color: #555555;\">&gt;::<\/span> type <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nT moduloOf(T t) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">%<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> moduloOf(<span style=\"color: #ff6600;\">5.5<\/span>);\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function template <span style=\"font-family: 'courier new', courier;\">moduloOf<\/span>&nbsp;requires that the&nbsp;<span style=\"font-family: 'courier new', courier;\">T<\/span>&nbsp;has to be integral. If T is not integral and the expression <span style=\"font-family: 'courier new', courier;\">std::is_integral&lt;T&gt;::value<\/span> evaluates to <span style=\"font-family: 'courier new', courier;\">false<\/span>, the failed substitution is not an error. The compiler removes the overload from the set of all potential overloads. After that step, there is no valid overload left.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5844\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/SFINAE.png\" alt=\"SFINAE\" width=\"600\" height=\"130\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/SFINAE.png 1294w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/SFINAE-300x65.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/SFINAE-1024x222.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/SFINAE-768x167.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>This technique is called SFINAE and stands for <strong>S<\/strong>ubstitution <strong>F<\/strong>ailure<strong> I<\/strong>s <strong>N<\/strong>ot <strong>A<\/strong>n <strong>E<\/strong>rror.&nbsp;<\/p>\n<p>Honestly, I only teach this technique in template classes. This does not hold for concepts. The issue becomes immediately obvious.<\/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;\">\/\/ requirementsCheckConcepts.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;concepts&gt;<\/span>\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>integral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> moduloOf(std<span style=\"color: #555555;\">::<\/span>integral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">%<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> moduloOf(<span style=\"color: #ff6600;\">5.5<\/span>);\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5845\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/conceptsError.png\" alt=\"conceptsError\" width=\"600\" height=\"226\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/conceptsError.png 1267w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/conceptsError-300x113.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/conceptsError-1024x386.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/02\/conceptsError-768x289.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"The_Definition_of_Templates_radically_improved\"><\/span>The Definition of Templates radically improved<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Thanks to the abbreviated function template syntax, the definition of a function template becomes a piece of cake. I already presented the new syntactic sugar in the function declarations of&nbsp;<span style=\"font-family: 'courier new', courier;\">addConstrained<\/span>, and <span style=\"font-family: 'courier new', courier;\">moduloOf. <\/span><span>Therefore, I skip the example.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Semantic_Categories\"><\/span>Semantic Categories<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Concepts do not stand for syntactic constraints but for semantic categories.&nbsp;<\/p>\n<p><span style=\"font-family: 'courier new', courier;\">Addable<\/span> is a concept that stands for a syntactic constraint.<\/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: #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\nconcept Addable <span style=\"color: #555555;\">=<\/span> has_plus<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad; insufficient<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>Addable N<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> algo(<span style=\"color: #006699; font-weight: bold;\">const<\/span> N<span style=\"color: #555555;\">&amp;<\/span> a, <span style=\"color: #006699; font-weight: bold;\">const<\/span> N<span style=\"color: #555555;\">&amp;<\/span> b) <span style=\"color: #0099ff; font-style: italic;\">\/\/ use two numbers<\/span>\r\n{\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">9<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> z <span style=\"color: #555555;\">=<\/span> algo(x, y);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ z = 16<\/span>\r\n\r\nstd::string xx <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"7\"<\/span>;\r\nstd::string yy <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"9\"<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> zz <span style=\"color: #555555;\">=<\/span> algo(xx, yy);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ zz = \"79\"<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I assume <span style=\"font-family: 'courier new', courier;\">Addable<\/span> behaves not like expected. The function template <code>algo<\/code> should accept arguments that model numbers and not just support the + operator.&nbsp;Consequentially, two strings can be used as arguments. This is bad because addition is commutative, but not string concatenation: <span style=\"font-family: 'courier new', courier;\">&#8220;7&#8221; + &#8220;9&#8221; != &#8220;9&#8221; + &#8220;7&#8221;.<\/span><\/p>\n<p>The solution is quite simple. Define the concept <code>Number.&nbsp;<\/code><span style=\"font-family: 'courier new', courier;\">Number<\/span> is a semantic category such as <span style=\"font-family: 'courier new', courier;\">Equal<\/span>, <span style=\"font-family: 'courier new', courier;\">Callable<\/span>, <span style=\"font-family: 'courier new', courier;\">Predicate<\/span>,&nbsp;or <span style=\"font-family: 'courier new', courier;\">Monad<\/span>&nbsp;are.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"My_Conclusion\"><\/span>My Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Of course, many arguments speak for an evolutionary step or a revolutionary jump in concepts. Mainly because of the semantic categories, I&#8217;m on the revolution side. Concepts such as Number, Equality, or Ordering remind me of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Plato\">Platon&#8217;s <\/a>words of ideas.&nbsp;It is revolutionary for me that we can now reason about programming in such categories.&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The ranges library that I will present in the<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-the-ranges-library\"> next post,<\/a> is the first customer of concepts. Ranges support algorithms that can<\/p>\n<ul>\n<li>operate directly on the container<\/li>\n<li>be evaluated lazily<\/li>\n<li>be composed<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let me conclude my series to concepts with this meta-post. Are concepts an evolution or a revolution in C++? The answer to this question bothered me quite a time.<\/p>\n","protected":false},"author":21,"featured_media":5808,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[415],"class_list":["post-5846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-concepts"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5846","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=5846"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5846\/revisions"}],"predecessor-version":[{"id":6756,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5846\/revisions\/6756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5808"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}