<story><title>C++20 Concepts: The Definitive Guide</title><url>https://thecodepad.com/cpp/c20-concepts-the-definitive-guide/</url></story><parent_chain><item><author>tialaramex</author><text>I feel like one of the things a &amp;quot;Definitive Guide&amp;quot; to this feature needs to make clear, and maybe even emphasise, is a key way these are different than say Rust type Traits.&lt;p&gt;Concepts, just like the SFINAE and constrexpr hacks you should discard in their favour, are about only what will &lt;i&gt;compile&lt;/i&gt; and you, the C++ programmer, are always responsible for shouldering the burden of deciding whether that will do what you meant &lt;i&gt;even if you have no insight into the types involved&lt;/i&gt;.&lt;p&gt;Example: In C++ the floating point type &amp;quot;float&amp;quot; matches a concept std::totally_ordered. You can, in fact, compile code that treats any container of floats as totally ordered. &lt;i&gt;But&lt;/i&gt; of course if some of them are NaN that won&amp;#x27;t work, because NaNs in fact don&amp;#x27;t even compare equal to themselves. You, the C++ programmer were responsible for knowing not to use this with NaNs.&lt;p&gt;Whereas, Rust&amp;#x27;s floating point type f32 implements PartialOrd (saying you can try to compare these) but not Ord (a claim that &lt;i&gt;all&lt;/i&gt; of them are totally ordered). If you know you won&amp;#x27;t use NaNs you can construct a wrapper type, and insist that is Ord and Rust will let you do that, because now &lt;i&gt;you&lt;/i&gt; pointed the gun at your foot, and it&amp;#x27;s clearly your fault if you pull the trigger.&lt;p&gt;This is a quite deliberate choice, it&amp;#x27;s not as though C++ could have just dropped in Rust-style traits, but I think a &amp;quot;Definitive Guide&amp;quot; ought to spell this out so that programmers understand that the burden the concept seems to be taking on is in fact still resting firmly on their shoulders in C++.&lt;p&gt;The other side of this is, if you wrote a C++ type say Beachball that implements the necessary comparison operators the Beachball is std::totally_ordered in C++ 20 with no further work from you to clear up this fact. Your users might hope you&amp;#x27;ll document whether Beachballs &lt;i&gt;are&lt;/i&gt; actually totally ordered or not though...&lt;p&gt;I think this will likely prove to be a curse, obviously its proponents think it will work out OK or even a blessing.</text></item></parent_chain><comment><author>CodeMage</author><text>&amp;gt; &lt;i&gt;I feel like one of the things a &amp;quot;Definitive Guide&amp;quot; to this feature needs to make clear, and maybe even emphasise, is a key way these are different than say Rust type Traits.&lt;/i&gt;&lt;p&gt;&amp;gt; &lt;i&gt;Concepts, just like the SFINAE and constrexpr hacks you should discard in their favour, are about only what will compile and you, the C++ programmer, are always responsible for shouldering the burden of deciding whether that will do what you meant even if you have no insight into the types involved.&lt;/i&gt;&lt;p&gt;To be fair, that&amp;#x27;s not what makes them different from Rust. Unless there&amp;#x27;s something in Rust that I missed, it offers no guarantees that the implementation of the trait is consistent with its semantics.&lt;p&gt;Whether it&amp;#x27;s traits or concepts, it&amp;#x27;s still about compile-time type-checking, not actual contracts.</text></comment>
<story><title>C++20 Concepts: The Definitive Guide</title><url>https://thecodepad.com/cpp/c20-concepts-the-definitive-guide/</url></story><parent_chain><item><author>tialaramex</author><text>I feel like one of the things a &amp;quot;Definitive Guide&amp;quot; to this feature needs to make clear, and maybe even emphasise, is a key way these are different than say Rust type Traits.&lt;p&gt;Concepts, just like the SFINAE and constrexpr hacks you should discard in their favour, are about only what will &lt;i&gt;compile&lt;/i&gt; and you, the C++ programmer, are always responsible for shouldering the burden of deciding whether that will do what you meant &lt;i&gt;even if you have no insight into the types involved&lt;/i&gt;.&lt;p&gt;Example: In C++ the floating point type &amp;quot;float&amp;quot; matches a concept std::totally_ordered. You can, in fact, compile code that treats any container of floats as totally ordered. &lt;i&gt;But&lt;/i&gt; of course if some of them are NaN that won&amp;#x27;t work, because NaNs in fact don&amp;#x27;t even compare equal to themselves. You, the C++ programmer were responsible for knowing not to use this with NaNs.&lt;p&gt;Whereas, Rust&amp;#x27;s floating point type f32 implements PartialOrd (saying you can try to compare these) but not Ord (a claim that &lt;i&gt;all&lt;/i&gt; of them are totally ordered). If you know you won&amp;#x27;t use NaNs you can construct a wrapper type, and insist that is Ord and Rust will let you do that, because now &lt;i&gt;you&lt;/i&gt; pointed the gun at your foot, and it&amp;#x27;s clearly your fault if you pull the trigger.&lt;p&gt;This is a quite deliberate choice, it&amp;#x27;s not as though C++ could have just dropped in Rust-style traits, but I think a &amp;quot;Definitive Guide&amp;quot; ought to spell this out so that programmers understand that the burden the concept seems to be taking on is in fact still resting firmly on their shoulders in C++.&lt;p&gt;The other side of this is, if you wrote a C++ type say Beachball that implements the necessary comparison operators the Beachball is std::totally_ordered in C++ 20 with no further work from you to clear up this fact. Your users might hope you&amp;#x27;ll document whether Beachballs &lt;i&gt;are&lt;/i&gt; actually totally ordered or not though...&lt;p&gt;I think this will likely prove to be a curse, obviously its proponents think it will work out OK or even a blessing.</text></item></parent_chain><comment><author>svalorzen</author><text>Are you sure about this? In my tests floating points are always considered partially ordered, not totally ordered. This page [0] even mentions this in the notes towards the bottom.&lt;p&gt;[0]: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;en.cppreference.com&amp;#x2F;w&amp;#x2F;cpp&amp;#x2F;utility&amp;#x2F;compare&amp;#x2F;partial_ordering&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;en.cppreference.com&amp;#x2F;w&amp;#x2F;cpp&amp;#x2F;utility&amp;#x2F;compare&amp;#x2F;partial_or...&lt;/a&gt;</text></comment>