{"id":962,"date":"2022-12-07T11:58:48","date_gmt":"2022-12-07T09:58:48","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=962"},"modified":"2022-12-07T14:40:02","modified_gmt":"2022-12-07T12:40:02","slug":"interface-requirements-in-c-using-c20-concepts","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2022\/12\/07\/interface-requirements-in-c-using-c20-concepts\/","title":{"rendered":"Interface requirements in C++ using C++20 concepts"},"content":{"rendered":"\n<p>When teaching data structures and algorithms in Java, I have implemented corresponding demonstrations in C++. So not to give too much solutions to the students, if I would be using Java in demos.<\/p>\n\n\n\n<p>When demonstrating a hash table, students are given this Java skeleton of a hash table class to implement, with key-value pairs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class KeyValueHashTable&lt;K extends Comparable&lt;K&gt;, V&gt; implements Dictionary&lt;K, V&gt; {<\/code><\/pre>\n\n\n\n<p>So, how to do something similar in C++ to keep the demo closer to the Java code? For hash table, the <code>Key<\/code> class (<code>K<\/code>) must implement the <code>Comparable<\/code> interface, as well as override the <code>hashCode()<\/code> and <code>equals()<\/code> inherited from <code>Object<\/code> class.<\/p>\n\n\n\n<p>I do not need to implement a <code>Comparable<\/code> interface in C++, since the operator overloading does what is necessary. But the Java <code>Object.hashCode()<\/code> is something I&#8217;d need to implement differently in C++, since in C++ there is no common base class to override the <code>hashCode()<\/code> from.<\/p>\n\n\n\n<p>Instead, I need a <code>Hashable<\/code> interface in C++ and all classes that would be used as the <code>Key<\/code> in the hash table, would have to implement the Hashable interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Hashable {\npublic:\n   virtual long hashCode() const = 0;\n   virtual ~Hashable() { }\n};<\/code><\/pre>\n\n\n\n<p>For example, a <code>Vehicle<\/code> class can implement <code>Hashable<\/code> interface and thus can act as a Key in hash table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle : public Hashable {\n\/\/ ...simple hash function, regNum being a std::string.\n   long hashCode() const override {\n      long hash = 5381;\n      for (auto c : regNum) {\n         hash = (hash &lt;&lt; 5) + hash + c;\n      }\n      return hash;\n   }<\/code><\/pre>\n\n\n\n<p>Luckily my C++ compiler supports C++20 with <strong><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/constraints\" target=\"_blank\" rel=\"noreferrer noopener\">concepts<\/a><\/strong>. Now I can say in the <code>HashTable<\/code> class that the classes to be used as <code>Key<\/code>s in the hash table must conform to the <code>Hashable<\/code> interface by providing a <code>CheckType<\/code> that does the checking:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;utility&gt;\n#include &lt;concepts&gt;\n\ntemplate &lt;class Type, class BaseClass&gt;\nconcept CheckType = std::is_base_of&lt;BaseClass, Type&gt;::value;<\/code><\/pre>\n\n\n\n<p>And then finally in the <code>HashTable<\/code>, explicitly say that the Key is required to pass this check that it inherits (implements) <code>Hashable<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;class K, class V&gt;\nrequires CheckType&lt;K, Hashable&gt;\nclass HashTable {<\/code><\/pre>\n\n\n\n<p>Now, if I would try to use <code>Vehicle<\/code> <em>without<\/em> it being a <code>Hashable<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle { \/\/ Not implementing the Hashable interface!\n...\nHashTable&lt;Vehicle, Location&gt; hashTable(20);\n\n\/\/ Will result in compiler error:\nConstraints not satisfied for class template 'HashTable' &#91;with K = Vehicle, V = Location]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When teaching data structures and algorithms in Java, I have implemented corresponding demonstrations in C++. So not to give too much solutions to the students, if I would be using Java in demos. When demonstrating a hash table, students are given this Java skeleton of a hash table class to implement, with key-value pairs: So, &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2022\/12\/07\/interface-requirements-in-c-using-c20-concepts\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Interface requirements in C++ using C++20 concepts&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[10,102,103,12],"class_list":["post-962","post","type-post","status-publish","format-standard","hentry","category-coding","tag-c","tag-concepts","tag-interfaces","tag-teaching"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=962"}],"version-history":[{"count":3,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/962\/revisions\/966"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}