{"id":524,"date":"2018-03-01T21:00:42","date_gmt":"2018-03-01T21:00:42","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=524"},"modified":"2023-05-31T16:19:10","modified_gmt":"2023-05-31T16:19:10","slug":"cpp20-concepts-eliminating-generics-paradigm-drawbacks","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/cpp20-concepts-eliminating-generics-paradigm-drawbacks\/","title":{"rendered":"C++20 Concepts: Eliminating Generics Paradigm Drawbacks"},"content":{"rendered":"<p>As Bjarne Stroustrup points out, \u201cC++ is a multi-paradigmed language.\u201d It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are structured programming, and generic programming. In the last few years C++ experts\u00a0like Andrei Alexandrescu, Scott Meyers and Herb Sutter promotes the uses of the generic programming and they qualify it as Modern C++ Design.<\/p>\n<p>Here\u2019s what say Andrei Alexandrescu about the Modern C++ design:<\/p>\n<pre>Modern C++ Design defines and systematically uses\u00a0<i>generic components<\/i>\u00a0- highly flexible design artifacts that are mixable and matchable to obtain rich behaviors with a small, orthogonal body of code.<\/pre>\n<p>Three\u00a0assertions are interesting in his point of view:<!--more--><\/p>\n<ul>\n<li>Modern C++ Design defines and\u00a0<strong>systematically uses\u00a0<\/strong><strong>generic components<\/strong>.<\/li>\n<li>Highly\u00a0<strong>flexible<\/strong>\u00a0design.<\/li>\n<li>Obtain rich behaviors with a\u00a0<strong>small, orthogonal<\/strong>\u00a0body of code.<\/li>\n<\/ul>\n<p>On the other side the OOP is very popular, the inheritance and the RTTI are two powerful mechanisms to design the C++ application and many developers prefer this paradigm in favor of the generic programming approach.<\/p>\n<p>Here&#8217;s the common definition of the inheritance:<\/p>\n<pre>In\u00a0object-oriented programming\u00a0(OOP),\u00a0<b>inheritance<\/b>\u00a0is when an\u00a0object\u00a0or\u00a0class\u00a0is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for\u00a0code reuse\u00a0and to allow independent extensions of the original software via public classes and interfaces.<\/pre>\n<p>Many C++ experts recommend to not overuse the inheritance and the dynamic polymorphism mechanism.\u00a0After all what&#8217;s wrong with the inheritance?<\/p>\n<p><em>Short answer<\/em>:\u00a0<strong>Very high coupling<\/strong>.<\/p>\n<p><b>Explanation:<\/b><br \/>\nLet&#8217;s take as example\u00a0the implementation of a class calculating a tax.<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-287\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics1.png\" alt=\"generics1\" width=\"475\" height=\"332\" \/><\/a><\/p>\n<p>The\u00a0CTaxCalculator collaborates only with the classes inheriting from\u00a0 the ICalculator \u00a0and can&#8217;t use any other not ICalculator class even if it can help\u00a0 to calculate the Tax. The calculator implementation class will be highly coupled with ICalculator and no possibility to use another class kind unless we introduce many other classes and interfaces to bypass this limitation. For example the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Adapter_pattern\">adapter pattern<\/a>\u00a0is a solution to bypass the inheritance high coupling issue. Think about it some GOF design patterns are there to resolve some issues generated by the inheritance high coupling issue.<\/p>\n<p><strong>Generic programming to the rescue<\/strong><\/p>\n<p>Using the generic programming, the same Tax calculator could be implemented like this:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-288\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics2.png\" alt=\"generics2\" width=\"479\" height=\"270\" \/><\/a><\/p>\n<p>The CGenericTaxCalculator class in the other side calculate the tax \u00a0by using\u00a0\u00a0any type capable to calculate the tax and it&#8217;s \u00a0not aware about its kind.\u00a0<strong>What&#8217;s important is the methods implemented by the type and not the class kind.\u00a0<\/strong>What\u00a0makes the generic programming more natural and flexible. Indeed in\u00a0the first implementation it&#8217;s like in the real world, a company searching for a developer\u00a0accept only ones graduated\u00a0from a specific school and reject all the others even if they\u00a0have the skills needed.<br \/>\nBut the flexibility comes with\u00a0a price, the code become hard to understand. Indeed in OOP programming I can just go to the definition of ICalculator to know what we wait for this type. However for the generic programming approach, it&#8217;s difficult to know what we expect exactly from the template parameter? Which members\u00a0must contains? Which constraints must be satisfied?<\/p>\n<p><strong>C++20 concepts to the rescue.<\/strong><\/p>\n<p>Here&#8217;s a short description of the C++20 concepts:<\/p>\n<pre><b>Concepts<\/b>\u00a0are an extension to\u00a0<a title=\"C++\" href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\">C++<\/a>'s\u00a0<a class=\"mw-redirect\" title=\"Template (programming)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Template_(programming)\">templates<\/a>, published as an ISO Technical Specification ISO\/IEC TS 19217:2015.<sup id=\"cite_ref-1\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/Concepts_(C%2B%2B)#cite_note-1\">[1]<\/a><\/sup>\u00a0They are named boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template (class template, function template, or member function of a class template), in which case it serves as a\u00a0<i>constraint<\/i>: it limits the set of arguments that are accepted as template parameters.<\/pre>\n<p>The concept feature\u00a0 was postponed many times. The good news is that the C++20 will includes this interesting feature.<\/p>\n<p>We can discover\u00a0this interesting <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/concept\">document<\/a>\u00a0 the motivation behind the concept feature;<\/p>\n<pre>The intent of concepts is to model semantic categories (Number, Range, RegularFunction) rather than syntactic restrictions (HasPlus, Array). According to\u00a0<a class=\"external text\" href=\"https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md#t20-avoid-concepts-without-meaningful-semantics\" rel=\"nofollow\">ISO C++ core guideline T.20<\/a>, \"The ability to specify a meaningful semantics is a defining characteristic of a true concept, as opposed to a syntactic constraint.\"<\/pre>\n<p>With concepts we can resolve the constraints specifications issue to make the code more readable and maintainable. It&#8217;s true that Boost provides for many years a concept implementation but adding them to the language will\u00a0make C++ more powerful and unique.<\/p>\n<p>Another most known drawback of generics programming\u00a0 is their error messages. Sometimes we can&#8217;t easily understand why an error is reported by the compiled and we can waste our time to know exactly why\u00a0 it&#8217;s not working as expected.<\/p>\n<p>Fortunately the concepts feature will also improve the error message as explained <a href=\"http:\/\/honermann.net\/blog\/2016\/07\/18\/refining-concepts-improving-error-messages\">here<\/a>.<\/p>\n<p><strong>Summary:<\/strong><\/p>\n<p>If the inheritance is overused when choosing the OOP approach, it&#8217;s introduce a high coupling between classes and force you to add more classes just to resolve this issue, it&#8217;s not the case when choosing to adopt the generic programming approach. And fortunately the missing piece to implement a readable, maintainable, low coupled code\u00a0will be part of the language soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As Bjarne Stroustrup points out, \u201cC++ is a multi-paradigmed language.\u201d It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are structured programming, and generic programming. In the last few years C++ experts\u00a0like Andrei Alexandrescu, Scott Meyers and Herb Sutter promotes the uses of &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/cpp20-concepts-eliminating-generics-paradigm-drawbacks\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C++20 Concepts: Eliminating Generics Paradigm Drawbacks&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,67,199,47,13,197,198],"class_list":["post-524","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-c-2","tag-c20","tag-concepts","tag-cpp","tag-generics","tag-generics-paragdims"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":9,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/524\/revisions"}],"predecessor-version":[{"id":1493,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/524\/revisions\/1493"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}