{"id":1542,"date":"2023-01-25T17:23:44","date_gmt":"2023-01-25T16:23:44","guid":{"rendered":"https:\/\/www.blauringlabs.com\/?p=1542"},"modified":"2023-01-26T10:59:14","modified_gmt":"2023-01-26T09:59:14","slug":"avoiding-unnecessary-comparison-operators-in-c20","status":"publish","type":"post","link":"https:\/\/www.blauringlabs.com\/de\/avoiding-unnecessary-comparison-operators-in-c20\/","title":{"rendered":"Avoiding unnecessary comparison operators in C++20"},"content":{"rendered":"<p>One advantage of the new c++20 standard is the possibility to program more with less code. This follows the principle of Don&#8217;t Repeat Yourself (aka DRY), which aims to avoid unnecesary code which makes our software more difficult -and therefore expensive- to maintain.<\/p>\n<p>A good example is when we need to compare two types and check if they are equals or not. <\/p>\n<p>In many software proyects, we and up with different types representing the same concept but implemented differently in different libraries.<\/p>\n<p>Let&#8217;s consider the following example: A struct representing a singe point with two coordinates:<\/p>\n<pre><code>struct Point {\n  int x;\n  int y;\n};<\/code><\/pre>\n<p>This is a common type in many libraries, so imagine we make use of a <em>NewPoint<\/em> struct in another place that represent the same concept:<\/p>\n<pre><code>struct NewPoint {\n  int a;\n  int b;\n};<\/code><\/pre>\n<p>At some point we need to check if two points are in fact the same. The compailer will complain if we use the operator == with objects of different types, so we need to write our own comparison:<\/p>\n<pre><code>struct NewPoint {\n  int a;\n  int b;\n\n  constexpr bool operator==(const Point&amp; rpoint) const noexcept\n  {\n      return (a == rpoint.x) &amp;&amp; (b == rpoint.y);\n  }\n};<\/code><\/pre>\n<p>This allows to compare a newPoint with a Point, but not the other way around. That is:<\/p>\n<pre><code> Point p = {2,4};\n NewPoint nP = {2,4};\n auto same = nP == p ? &quot;yes&quot; : &quot;no&quot;;\n std::cout &lt;&lt; same  &lt;&lt; &quot;\\n&quot;;<\/code><\/pre>\n<p>This code compiles and return <em>yes<\/em>, but we&#8217;re comparing a <em>NewPoint<\/em> with a <em>Point<\/em>, which means, if we write the comparison in this way:<\/p>\n<pre><code> auto same = p == nP ? &quot;yes&quot; : &quot;no&quot;;<\/code><\/pre>\n<p>It won&#8217;t compile in C++17. We need to impement the operator== in <em>Point<\/em> as well, which would be 95% identical to the previous one.<\/p>\n<p>However, in C++20 the compiler <strong>understand the mathematical sense of the identity<\/strong> and is able to invoke the operator== of NewPoint, event though <em>p<\/em> is of type <em>Point<\/em>!<\/p>\n<p>This means that we don&#8217;t have to implement a dedicated operator== in <em>Point<\/em>, as the compiler knows that if <em>nP== p<\/em>, then <em>p == nP<\/em> !<\/p>\n<p>This avoids the need of a dedicated operator== in <em>Point<\/em> and follows the DRY principle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One advantage of the new c++20 standard is the possibility to program more with less code. This follows the principle of Don&#8217;t Repeat Yourself (aka DRY), which aims to avoid unnecesary code which makes our software more difficult\u2026<\/p>\n","protected":false},"author":2,"featured_media":1557,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pgc_meta":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-1542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/posts\/1542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/comments?post=1542"}],"version-history":[{"count":15,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/posts\/1542\/revisions"}],"predecessor-version":[{"id":1560,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/posts\/1542\/revisions\/1560"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/media\/1557"}],"wp:attachment":[{"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/media?parent=1542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/categories?post=1542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blauringlabs.com\/de\/wp-json\/wp\/v2\/tags?post=1542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}