{"id":6030,"date":"2020-11-17T21:21:20","date_gmt":"2020-11-17T21:21:20","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/safe-comparisons-of-integrals-with-c-20\/"},"modified":"2020-11-17T21:21:20","modified_gmt":"2020-11-17T21:21:20","slug":"safe-comparisons-of-integrals-with-c-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/safe-comparisons-of-integrals-with-c-20\/","title":{"rendered":"Safe Comparisons of Integrals with C++20"},"content":{"rendered":"<p>&nbsp;When you compare signed and unsigned integers, you may not get the result you expect. Thanks to the six <code>std::cmp_<\/code>* functions, there is a cure in C++20.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png\" alt=\"TimelineCpp20CoreLanguage\" width=\"650\" height=\"262\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Maybe, you remember the rule &#8220;<a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-mix\">ES.100 Don&#8217;t mix signed and unsigned arithmetic&#8221; <\/a>from the C++ Core Guidelines. I wrote a few words about it in my previous post on &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-to-statements-and-arithmetic-rules\">Arithmetic Rules<\/a>&#8220;. Today, I want to investigate this issue and compare signed and unsigned integers.<\/p>\n<p>Let&#8217;s start with an unsafe comparison.<\/p>\n<h2>Unsafe Comparison of Integrals<\/h2>\n<p>&nbsp;Of course, there is a reason for the program name <code>unsafeComparison.cpp<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ unsafeComparison.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3<\/span>;                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 &lt; 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (x <span style=\"color: #555555;\">&lt;<\/span> y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 &lt;= 7: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (x <span style=\"color: #555555;\">&lt;=<\/span> y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 &gt; 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (x <span style=\"color: #555555;\">&gt;<\/span> y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 =&gt; 7: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (x <span style=\"color: #555555;\">&gt;=<\/span> y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When I execute the program, the output may not meet your expectations.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6021\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison.png\" alt=\"unsafeComparison\" width=\"400\" height=\"234\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison.png 464w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison-300x175.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>When you read the program output, you recognize -3 should be bigger than 7. You presumably know the reason. I compared a <code>signed x<\/code> (line (1)) with an<code> unsigned y<\/code> (line (2)). What is happening under the hood? The following program provides the answer.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ unsafeComparison2.cpp<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> val <span style=\"color: #555555;\">=<\/span> x <span style=\"color: #555555;\">&lt;<\/span> y;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    static_assert(<span style=\"color: #006699; font-weight: bold;\">static_cast<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3<\/span>) <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">4'294'967'293<\/span>);\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In the example, I&#8217;m focusing on the less-than-operator. <a href=\"https:\/\/cppinsights.io\/s\/62732a01\">C++ Insights<\/a> gives me the following output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6022\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison2.png\" alt=\"unsafeComparison2\" width=\"650\" height=\"140\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison2.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison2-300x65.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison2-1024x221.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/unsafeComparison2-768x166.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Here is what&#8217;s happening:<\/p>\n<ol>\n<li>The compiler transforms the expression<code> x &lt; y <\/code>(line 1) into <code>static_cast&lt;unsigned int&gt;(x) &lt; y<\/code>. In particular, the <code>signed<\/code> <code>x<\/code> is converted to an unsigned int.<\/li>\n<li>Due to the conversion,<code> -3 <\/code>becomes 4&#8217;294&#8217;967&#8217;293.&nbsp;<\/li>\n<li><code> 4'294'967'293<\/code> is equal to (-3) modulo (2 to the power of 32).<\/li>\n<li>32 is the number of bits of an unsigned<code> int<\/code> on C++ Insights.\n<\/li>\n<\/ol>\n<p>Thanks to C++20, we have a safe comparison of integrals.<\/p>\n<\/p>\n<h2>Safe Comparison of Integrals<\/h2>\n<p>C++20 supports the six comparison functions for integrals:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6023\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/compareFunctionsEng1.png\" alt=\"compareFunctionsEng1\" width=\"568\" height=\"294\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/compareFunctionsEng1.png 568w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/compareFunctionsEng1-300x155.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/p>\n<p>Thanks to the six comparison functions, I can easily transform the previous program<code> unsafeComparison.cpp<\/code> into the program <code>safeComparison.cpp. <\/code>The new comparison functions require the header <code>&lt;utility<\/code>&gt;.<code><br \/><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ safeComparison.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"3 == 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_equal(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"3 != 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_not_equal(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 &lt; 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_less(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 &lt;= 7: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_less_equal(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 &gt; 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_greater(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3 =&gt; 7: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_greater_equal(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I also used in this program the equal and not equal operators.<\/p>\n<p>Thanks to GCC 10, here is the expected result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6024\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/safeComparison.png\" alt=\"safeComparison\" width=\"200\" height=\"176\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Invoking a comparison function with a non-integral value would cause a compile-time error.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ safeComparison2.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">double<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3.5<\/span>;             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"-3.5 &lt; 7:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>cmp_less(x, y) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Trying to compare a <code>double<\/code> (line (1)) and an <code>unsigned int<\/code> (line (2)) gives the GCC 10 compiler a lengthy error message. Here is the crucial line of the error message:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6025\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/safeComparison2.png\" alt=\"safeComparison2\" width=\"650\" height=\"56\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/safeComparison2.png 1159w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/safeComparison2-300x26.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/safeComparison2-1024x87.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/safeComparison2-768x66.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/>The internal type-traits&nbsp; __is_standard_integer failed. I was curious about what that means and looked it up in the GCC <a href=\"https:\/\/github.com\/gcc-mirror\/gcc\/blob\/master\/libstdc%2B%2B-v3\/include\/std\/type_traits\">type-traits implementation on GitHub<\/a>. Here are the relevant lines from the header<code> type-traits:<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ Check if a type is one of the signed or unsigned integer types.<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> _Tp<span style=\"color: #555555;\">&gt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> __is_standard_integer\r\n      <span style=\"color: #555555;\">=<\/span> __or_<span style=\"color: #555555;\">&lt;<\/span>__is_signed_integer<span style=\"color: #555555;\">&lt;<\/span>_Tp<span style=\"color: #555555;\">&gt;<\/span>, __is_unsigned_integer<span style=\"color: #555555;\">&lt;<\/span>_Tp<span style=\"color: #555555;\">&gt;&gt;<\/span>;\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Check if a type is one of the signed integer types.<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> _Tp<span style=\"color: #555555;\">&gt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> __is_signed_integer <span style=\"color: #555555;\">=<\/span> __is_one_of<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">__remove_cv_t<\/span><span style=\"color: #555555;\">&lt;<\/span>_Tp<span style=\"color: #555555;\">&gt;<\/span>,\r\n\t  <span style=\"color: #007788; font-weight: bold;\">signed<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span>, <span style=\"color: #007788; font-weight: bold;\">signed<\/span> <span style=\"color: #007788; font-weight: bold;\">short<\/span>, <span style=\"color: #007788; font-weight: bold;\">signed<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">signed<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span>,\r\n\t  <span style=\"color: #007788; font-weight: bold;\">signed<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Check if a type is one of the unsigned integer types.<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> _Tp<span style=\"color: #555555;\">&gt;<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> __is_unsigned_integer <span style=\"color: #555555;\">=<\/span> __is_one_of<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">__remove_cv_t<\/span><span style=\"color: #555555;\">&lt;<\/span>_Tp<span style=\"color: #555555;\">&gt;<\/span>,\r\n\t  <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span>, <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">short<\/span>, <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span>,\r\n\t  <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>__remove_cv_t<\/code> is the internal function of GCC to remove <code>const<\/code> or <code>volatile<\/code> from a type.<\/p>\n<p>Maybe, you are now curious about what happens when you compare a <code>double<\/code> and an <code>unsigned int<\/code> the classical way.<\/p>\n<p>Here is the modified program <code>safeComparison2.cpp.<\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ classicalComparison.cpp<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">double<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3.5<\/span>;             \r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;          \r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> x <span style=\"color: #555555;\">&lt;<\/span> y;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ true<\/span>\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>It works. The crucial <code>unsigned int<\/code> is <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/implicit_conversion\">floating-point promoted<\/a> to <code>double<\/code>. <a href=\"https:\/\/cppinsights.io\/s\/62c279f7\">C++ Insights<\/a> shows the truth:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6026\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/classicalComparision.png\" alt=\"classicalComparision\" width=\"400\" height=\"158\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/classicalComparision.png 466w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/classicalComparision-300x119.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>After so many comparisons, I want to end this post with our new mathematical constants with C++20.<\/p>\n<h2>Mathematical Constants<\/h2>\n<p>&nbsp;First, the constants require the header <code>&lt;numbers&gt;<\/code> and the namespace <code>std::numbers<\/code>. The following tables give you the first overview. <code><\/code><code><\/code><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" alignright size-full wp-image-6027\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/constantsEng2.png\" alt=\"constantsEng2\" width=\"350\" height=\"280\" style=\"float: right;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/constantsEng2.png 598w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/constantsEng2-300x240.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><img loading=\"lazy\" decoding=\"async\" class=\" alignleft size-full wp-image-6028\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/constantsEng1.png\" alt=\"constantsEng1\" width=\"250\" height=\"273\" style=\"float: left;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/constantsEng1.png 429w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/constantsEng1-275x300.png 275w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The program <code>mathematicConstants.cpp<\/code> applies the mathematical constants.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ mathematicConstants.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;numbers&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout<span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>setprecision(<span style=\"color: #ff6600;\">10<\/span>);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::e: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>e <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::log2e: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>log2e <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::log10e: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>log10e <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::pi: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>pi <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::inv_pi: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>inv_pi <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::inv_sqrtpi: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>inv_sqrtpi <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::ln2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>ln2 <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::sqrt2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>sqrt2 <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::sqrt3: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>sqrt3 <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::inv_sqrt3: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>inv_sqrt3 <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::egamma: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>egamma <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::numbers::phi: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>numbers<span style=\"color: #555555;\">::<\/span>phi <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Here is the output of the program with the MSVC compiler 19.27.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6029\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/mathematicalConstants.png\" alt=\"mathematicalConstants\" width=\"400\" height=\"333\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/mathematicalConstants.png 437w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/mathematicalConstants-300x250.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The mathematical constants are available for <code>float<\/code>, <code>double<\/code>, and <code>long double<\/code>. Per-default <code>double<\/code> is used, but you can also specify <code>float<\/code> (<code>std::numbers::pi_v&lt;float&gt;<\/code>) or<code> long double<\/code> (<code>std::numbers::pi_v&lt;long double&gt;<\/code>).<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>C++20 offers more valuable utilities. For example, you can ask your compiler which C++ feature it supports, and can easily create functional objects with <code>std::bind_front, <\/code>or perform different actions in a function whether the function runs a compile-time or at runtime.<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;When you compare signed and unsigned integers, you may not get the result you expect. Thanks to the six std::cmp_* functions, there is a cure in C++20.<\/p>\n","protected":false},"author":21,"featured_media":5945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[],"class_list":["post-6030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6030"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6030\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}