{"id":6353,"date":"2022-04-27T14:11:38","date_gmt":"2022-04-27T14:11:38","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/using-requires-expression-in-c-20-as-a-standalone-feature\/"},"modified":"2023-06-26T09:07:20","modified_gmt":"2023-06-26T09:07:20","slug":"using-requires-expression-in-c-20-as-a-standalone-feature","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/using-requires-expression-in-c-20-as-a-standalone-feature\/","title":{"rendered":"Using Requires Expression in C++20 as a Standalone Feature"},"content":{"rendered":"<p>In my last post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/defining-concepts-with-requires-expressions\">Defining Concepts with Requires Expressions<\/a>&#8220;, I exemplified how you can use requires expressions to define concepts. Requires expressions can also be used as a standalone feature when a compile-time predicate is required.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5808\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts.png\" alt=\"TimelineCpp20Concepts\" width=\"650\" height=\"255\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts.png 954w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts-300x118.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/11\/TimelineCpp20Concepts-768x301.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Typical use cases for compile-time predicates are<code> static_assert<\/code>, <code>constexpr if<\/code>, or a requires clause. A compile&#8211;time predicate is an expression that returns at compile time a boolean. Let me start this post with C++11.<\/p>\n<h2><code>static_assert<\/code><\/h2>\n<div>\n<div><code>static_assert<\/code> requires a compile-time predicate, and a message is displayed when the compile-time predicate fails. With C++17, the message is optional. With C++20, this compile-time predicates can be a requires expression.<\/div>\n<div>&nbsp;<\/div>\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;\">\/\/ staticAssertRequires.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;concepts&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Fir {                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> count() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">2020<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Sec {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> size() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">2021<\/span>;\r\n    }\r\n};\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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n   \r\n    Fir fir;\r\n    static_assert(requires(Fir fir){ { fir.count() } <span style=\"color: #555555;\">-&gt;<\/span> std<span style=\"color: #555555;\">::<\/span>convertible_to<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; });     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n    Sec sec;\r\n    static_assert(requires(Sec sec){ { sec.count() } <span style=\"color: #555555;\">-&gt;<\/span> std<span style=\"color: #555555;\">::<\/span>convertible_to<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; });     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> third;\r\n    static_assert(requires(<span style=\"color: #007788; font-weight: bold;\">int<\/span> third){ { third.count() } <span style=\"color: #555555;\">-&gt;<\/span> std<span style=\"color: #555555;\">::<\/span>convertible_to<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; }); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<\/div>\n<div>&nbsp;<\/div>\n<div>\n<div>\n<div>The requires expressions (lines 1, 2, and 3) check if the object has a member function <code>count<\/code> and if its result is convertible to<code> int<\/code>. This check is only valid for the class <code>First<\/code> (lines 4). On the contrary, the checks in lines (2) and (3) fail.<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6348\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/staticAssertRequires.png\" alt=\"staticAssertRequires\" width=\"600\" height=\"163\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/staticAssertRequires.png 1086w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/staticAssertRequires-300x81.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/staticAssertRequires-1024x278.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/staticAssertRequires-768x209.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<\/div>\n<div>\n<div>Maybe, you want to compile code depending on a compile-time check. In this case, the C++17 feature <code>constexpr if<\/code> combined with requires expressions provides you with the necessary tool.<\/div>\n<div>&nbsp;<\/div>\n<div><\/div>\n<h2><code>constexpr if<\/code><\/h2>\n<div>\n<div><code>constexpr if<\/code> allows it to compile source code conditionally. For the condition, the requires expression comes into play. All branches of the if statement have to be valid.<\/div>\n<p><\/p>\n<div>Thanks to <code>constexpr if<\/code>, you can define functions that inspect their arguments at compile time and generate different functionality based on their analysis.<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<\/div>\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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ constexprIfRequires.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;concepts&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> First {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> count() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">2020<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Second {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> size() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">2021<\/span>;\r\n    }\r\n};\r\n\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> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> getNumberOfElements(T t) {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (requires(T t){ { t.count() } <span style=\"color: #555555;\">-&gt;<\/span> std<span style=\"color: #555555;\">::<\/span>convertible_to<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; }) {   <span style=\"color: #0099ff;\"><em>\/\/ (1)<\/em><\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> t.count();\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (requires(T t){ { t.size() } <span style=\"color: #555555;\">-&gt;<\/span> std<span style=\"color: #555555;\">::<\/span>convertible_to<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>; }) {   <span style=\"color: #0099ff;\"><em> \/\/ (2)<\/em><\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> t.size();\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">42<\/span>;                                                               <span style=\"color: #0099ff;\"> <em>\/\/ (3)<\/em><\/span>\r\n\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n   \r\n    First first;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getNumberOfElements(first): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> getNumberOfElements(first) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    Second second;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getNumberOfElements(second): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> getNumberOfElements(second) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> third;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getNumberOfElements(third): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getNumberOfElements(third) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<div>Lines (1) and (2) are crucial in this code example. In line (1), the requires expressions determine if the variable <code>t<\/code> has a member function <code>count<\/code> that returns an <code>int<\/code>. Accordingly, line (2) determines if the variable<code> t<\/code> has a member function <code>size<\/code>. The else statement in line (3) is applied as a fallback.<\/div>\n<div>&nbsp;<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6349\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/constexprIfRequires.png\" alt=\"constexprIfRequires\" width=\"450\" height=\"223\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/constexprIfRequires.png 516w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/constexprIfRequires-300x149.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/div>\n<h2>Requires Clause<\/h2>\n<p>First of all, I have to answer the question: What is a requires clause?<\/p>\n<p>There are essentially four ways to use a concept, such as <code>std::integral<\/code>.<\/p>\n<\/div>\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;\">\/\/ conceptsIntegralVariations.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;concepts&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;      <\/span>\r\n\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> T<span style=\"color: #555555;\">&gt;<\/span>                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)                          <\/span>\r\nrequires std<span style=\"color: #555555;\">::<\/span>integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>                     \r\nauto gcd(T a, T b) {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span>( b <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span> ) <span style=\"color: #006699; font-weight: bold;\">return<\/span> a;\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc00ff;\">gcd<\/span>(b, a <span style=\"color: #555555;\">%<\/span> b);\r\n}\r\n\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> T<span style=\"color: #555555;\">&gt;<\/span>                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)        <\/span>\r\nauto gcd1(T a, T b) requires std<span style=\"color: #555555;\">::<\/span>integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> {  \r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span>( b <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span> ) <span style=\"color: #006699; font-weight: bold;\">return<\/span> a; \r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc00ff;\">gcd1<\/span>(b, a <span style=\"color: #555555;\">%<\/span> b);\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>integral T<span style=\"color: #555555;\">&gt;<\/span>                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)        <\/span>\r\nauto gcd2(T a, T b) {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span>( b <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span> ) <span style=\"color: #006699; font-weight: bold;\">return<\/span> a; \r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc00ff;\">gcd2<\/span>(b, a <span style=\"color: #555555;\">%<\/span> b);\r\n}\r\n                                           \r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> gcd3(std<span style=\"color: #555555;\">::<\/span>integral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> a, <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n          std<span style=\"color: #555555;\">::<\/span>integral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> b) { \r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span>( b <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span> ) <span style=\"color: #006699; font-weight: bold;\">return<\/span> a; \r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc00ff;\">gcd3<\/span>(b, a <span style=\"color: #555555;\">%<\/span> b);\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"gcd(100, 10)= \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span>  gcd(<span style=\"color: #ff6600;\">100<\/span>, <span style=\"color: #ff6600;\">10<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"gcd1(100, 10)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  gcd1(<span style=\"color: #ff6600;\">100<\/span>, <span style=\"color: #ff6600;\">10<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"gcd2(100, 10)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  gcd2(<span style=\"color: #ff6600;\">100<\/span>, <span style=\"color: #ff6600;\">10<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"gcd3(100, 10)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  gcd3(<span style=\"color: #ff6600;\">100<\/span>, <span style=\"color: #ff6600;\">10<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<div>Thanks to the header<code> &lt;concepts<\/code>&gt; I can use the concept <code>std::integral<\/code>. The concept is fulfilled if <code>T<\/code> it is <a href=\"https:\/\/www.modernescpp.com\/https:\/en.cppreference.com\/w\/cpp\/types\/is_integral\">integral<\/a>. The function name <code>gcd<\/code> stands for the greatest-common-divisor algorithm based on the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Euclid\">Euclidean<\/a> algorithm.<\/div>\n<p><\/p>\n<div>Here are the four ways to use concepts:<\/div>\n<\/div>\n<ol>\n<li>&nbsp;Requires clause (line 1)<\/li>\n<li>Trailing requires clause (line 2)<\/li>\n<li>Constrained template parameter (line 3)<\/li>\n<li>Abbreviated function template (line 4)<\/li>\n<\/ol>\n<div>\n<div>For simplicity reasons, each function template returns <code>auto<\/code>. There is a semantic difference between the function templates<code> gcd, gcd1, gcd2<\/code>, and the function <code>gcd3<\/code>. In the case of <code>gcd, gcd1, or gcd2<\/code>, arguments <code>a<\/code> and <code>b<\/code> must have the same type. This does not hold for the function <code>gcd3<\/code>. Parameters<code> a<\/code> and<code> b<\/code> can have different types but must both fulfill the concept <code>std::integral.<\/code><\/div>\n<div>&nbsp;<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6350\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/conceptsIntegralVariationsLocal.png\" alt=\"conceptsIntegralVariations\" width=\"500\" height=\"250\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/conceptsIntegralVariationsLocal.png 558w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/conceptsIntegralVariationsLocal-300x150.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<div>The functions <code>gcd<\/code> and <code>gcd1<\/code> use requires clauses.<\/div>\n<div>&nbsp;<\/div>\n<div>There is an interesting fact about requires clauses. You can use any compile-time predicate as an expression. I check in the following requirces clause if an int as a non-type template parameter is smaller than 20.<\/div>\n<div>&nbsp;<\/div>\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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ requiresClause.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/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> i<span style=\"color: #555555;\">&gt;<\/span>\r\nrequires (i <span style=\"color: #555555;\">&lt;=<\/span> <span style=\"color: #ff6600;\">20<\/span>)            <em><span style=\"color: #0099ff;\"> \/\/ (1)<\/span><\/em>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> sum(<span style=\"color: #007788; font-weight: bold;\">int<\/span> j) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">+<\/span> j;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum&lt;20&gt;(2000): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sum<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">20<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2000<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>,\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ std::cout &lt;&lt; \"sum&lt;23&gt;(2000): \" &lt;&lt; sum&lt;23&gt;(2000) &lt;&lt; '\\n',  \/\/ ERROR <\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<div>&nbsp;<\/div>\n<div>\n<div>\n<div>The compile-time predicate used in line (1) exemplifies an interesting point: the requirement is applied to the non-type<code> i<\/code>, and not on a type as usual.<\/div>\n<div>&nbsp;<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6351\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/requiresClause.png\" alt=\"requiresClause\" width=\"200\" height=\"43\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/div>\n<div>&nbsp;<\/div>\n<div>\n<div>When you use the commented-out line in the <code>main<\/code> program, the clang compiler reports the following error:<\/div>\n<div>&nbsp;<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6352\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/requiresClauseError.png\" alt=\"requiresClauseError\" width=\"600\" height=\"160\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/requiresClauseError.png 924w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/requiresClauseError-300x80.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/requiresClauseError-768x204.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>&nbsp;<\/div>\n<div>Here are more details about non-type template parameters: &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/alias-templates-and-template-parameters\">Alias Templates and Template Parameters<\/a>&#8220;.<\/div>\n<div>&nbsp;<\/div>\n<div>Typically, you use a concept in a requires clause, but there is more: <code>requires requires<\/code> or anonymous concepts<\/div>\n<h3><code>requires requires<\/code> or anonymous concepts<\/h3>\n<div>&nbsp;<\/div>\n<div>\n<div>\n<div>You can define an anonymous concept and directly use it. In general, you should not do it. Anonymous concepts make your code hard to read, and you cannot reuse your concepts.<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<\/div>\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: 0px; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n    requires <span style=\"color: #ff0000;\">requires (T x) { x + x; } <\/span>\r\nauto add(T a, T b) { <br \/><span style=\"color: #006699; font-weight: bold;\">    return<\/span> a <span style=\"color: #555555;\">+<\/span> b; <br \/>}\r\n<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<div>The function template defines its concept as ad-hoc. The function template <code>add <\/code>uses a requires expression (<span style=\"color: #ff0000;\"><code>requires(T x) { x + x; }<\/code><\/span> ) inside a requires clause. The anonymous concept is equivalent to the following concept Addable.<\/div>\n<div>&nbsp;<\/div>\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: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\nconcept Addable <span style=\"color: #555555;\">=<\/span> requires (T a, T b) {\r\n    a <span style=\"color: #555555;\">+<\/span> b; \r\n};\r\n<\/pre>\n<\/div>\n<div>&nbsp;<\/div>\n<div>Consequentially, the following four implementations of the function template <code>add<\/code> are equivalent to the previous one:<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;<\/div>\n<\/div>\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: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>  <span style=\"color: #0099ff; font-style: italic;\">\/\/ requires clause<\/span>\r\n    requires Addable<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> add(T a, T b) { \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b; \r\n}\r\n\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> T<span style=\"color: #555555;\">&gt;<\/span>  <span style=\"color: #0099ff; font-style: italic;\">\/\/ trailing requires clause<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> add(T a, T b) requires Addable<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> { \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b; \r\n} \r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span>Addable T<span style=\"color: #555555;\">&gt;<\/span>   <span style=\"color: #0099ff; font-style: italic;\">\/\/ constrained template parameter<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> add(T a, T b){ \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b; \r\n} \r\n                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ abbreviated function template<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> add(Addable <span style=\"color: #006699; font-weight: bold;\">auto<\/span> a, Addable <span style=\"color: #006699; font-weight: bold;\">auto<\/span> b) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>As a short reminder: The last implementation based on abbreviated function templates syntax can deal with values having different types.<\/p>\n<p>Again, I want to emphasize it:<strong> Concepts should encapsulate general ideas and give them a self-explanatory name for reuse. They are invaluable for maintaining code. Anonymous concepts read more like syntactic constraints on template parameters and should be avoided.<\/strong><\/p>\n<h2>What&#8217;s next?<\/h2>\n<div>\n<div>Using a concept in<code> static_assert(Concept&lt;T&gt;<\/code>) tests whether the type<code> T<\/code> fulfills the concept. Let&#8217;s see how we can use this in my next post.<\/div>\n<div>&nbsp;<\/div>\n<div><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post &#8220;Defining Concepts with Requires Expressions&#8220;, I exemplified how you can use requires expressions to define concepts. Requires expressions can also be used as a standalone feature when a compile-time predicate is required.<\/p>\n","protected":false},"author":21,"featured_media":5808,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[415,418],"class_list":["post-6353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-concepts","tag-requires-expressions"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6353","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=6353"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6353\/revisions"}],"predecessor-version":[{"id":6671,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6353\/revisions\/6671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5808"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}