{"id":1,"date":"2018-09-28T15:59:04","date_gmt":"2018-09-28T13:59:04","guid":{"rendered":"http:\/\/devwork.space\/private_html\/?p=1"},"modified":"2018-10-10T01:56:19","modified_gmt":"2018-10-09T23:56:19","slug":"concepts","status":"publish","type":"post","link":"https:\/\/devwork.space\/?p=1","title":{"rendered":"Introducing concepts in ledboard project"},"content":{"rendered":"<p>In near feature we&#8217;ll able to write nifty constrains for template arguments using concepts (C++20). I&#8217;m currently researching common use cases and trying to adopt them in my <a href=\"https:\/\/github.com\/dev-0x7C6\">ledboard<\/a> project. With GCC &gt;= 8.1 you can enable concepts using `-fconcepts` instrumentation compile flag, but this feature is not standardized yet. The current implementation and specification on concepts might change, keep this in mind. With recent talk at CppCon by Bjarne Stroustrup we get a pretty bold picture that this feature is essential and simplify things (see <a href=\"https:\/\/www.youtube.com\/watch?v=HddFGPTAmtU\">Bjarne Stroustrup \u201cConcepts: The Future of Generic Programming (the future is here)\u201d<\/a>). From my perspective them main advantage is to write zero-cost abstraction with better constrains and maintainability and get rid of template error novel.<\/p>\n<p>Below there is code that I&#8217;m currently using for as static interface for my animation classes, take a look:<\/p>\n<pre id=\"1538359293773.277\" class=\"prettyprint\" style=\"padding: 10px; border: 1px solid rgba(231, 231, 230, 1); border-radius: 10px; overflow: auto; background-color: #ffffff; font-size: 13px;\" data-highlight=\"\">template&lt;typename type&gt;\r\nconcept bool animation_interface = requires(type a) {\r\n    { a.value() } -&gt; rgb \/\/ animation_interface must have method value() that returns rgb type\r\n    { a.r() } -&gt; u8 \/\/ requires a.r() method with integer return type \r\n    { a.g() } -&gt; u8 \/\/ ...\r\n    { a.b() } -&gt; u8 \/\/ ...\r\n    { a.step() } -&gt; void \/\/ requires callable a.step() method without return type\r\n};\r\n\r\n\/\/ without constrains\r\ntemplate &lt;typename type&gt;\r\nconstexpr auto doSomething(type &amp;&amp;v) { return v.value(); }\r\n\r\ndoSomething(100) \/\/ deduction from arguments to doSomething&lt;int&gt;(100)\r\n\/\/ will fail with error we cannot call value() on 100 (prvalue)\r\n\/\/ but error is hard to understand and situation is a post factum\r\n\r\n\r\n\/\/ with constrains\r\ntemplate &lt;animation_interface type&gt;\r\nconstexpr auto doSomething(type &amp;&amp;v) { return v.value(); }\r\n\r\ndoSomething(100) \/\/ does not fulfilled requirements for animation_interface (nice error)\r\n\r\n\/\/ example \r\nclass white_color_forever {\r\npublic:\r\n    auto value() -&gt; rgb { return {r(), g(), b()}; } \r\n    auto r() { return 0xff; }\r\n    auto g() { return 0xff; }\r\n    auto b() { return 0xff; }\r\n    auto step() -&gt; void {}\r\n}\r\n\r\ndoSomething(white_color_forever{}); \/\/ fine, this is acceptable because \r\n\/\/ all exceptations are meet\r\n\r\n\r\n\r\n<input id=\"772.3773929538351\" class=\"cdbx-try-code cdbx-btn-main\" style=\"background-color: #ffffff; margin-bottom: 0; color: #008b8b; border: 1px solid rgba(231, 231, 230, 1); border-radius: 10px; font-size: 13px; height: 30px; min-width: 60px; max-width: 150px; padding: 4px; font-weight: normal; outline: none; display: none; float: right;\" type=\"button\" value=\"Run\" data-code=\"1538359293773.277\" data-highlight=\"\" data-lang=\"1\" data-filename=\"Filename\" \/><\/pre>\n<p>There are requirements that must be fulfilled. Code does not compile if template parameter do not fulfill requirements. If you&#8217;re interested there is one of my use cases: a <a href=\"https:\/\/github.com\/dev-0x7C6\/ledboard\/commit\/5f46cca768d5e473f29bb730d9da9bfdf500c1c1\">palette_converter_wrapper<\/a>. Where I&#8217;m wrapping animation inside method forwarding class and overloading <strong>value()<\/strong> method. This code will play nice with any type that fulfill <strong>abstraction_interface<\/strong> requirements \ud83d\ude42 So this is a static interface, <span class=\"pFQiGc\">that is what we&#8217;re waiting for<\/span>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In near feature we&#8217;ll able to write nifty constrains for template arguments using concepts (C++20). I&#8217;m currently researching common use cases and trying to adopt them in my ledboard project. With GCC &gt;= 8.1 you can enable concepts using `-fconcepts` instrumentation compile flag, but this feature is not standardized yet. The current implementation and specification &hellip; <a href=\"https:\/\/devwork.space\/?p=1\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Introducing concepts in ledboard project&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3,2],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii","tag-c20","tag-concepts"],"_links":{"self":[{"href":"https:\/\/devwork.space\/index.php?rest_route=\/wp\/v2\/posts\/1"}],"collection":[{"href":"https:\/\/devwork.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devwork.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devwork.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devwork.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1"}],"version-history":[{"count":17,"href":"https:\/\/devwork.space\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":84,"href":"https:\/\/devwork.space\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions\/84"}],"wp:attachment":[{"href":"https:\/\/devwork.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devwork.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devwork.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}