{"id":6347,"date":"2022-04-20T21:01:48","date_gmt":"2022-04-20T21:01:48","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/defining-concepts-with-requires-expressions\/"},"modified":"2023-06-26T09:07:44","modified_gmt":"2023-06-26T09:07:44","slug":"defining-concepts-with-requires-expressions","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/defining-concepts-with-requires-expressions\/","title":{"rendered":"Defining Concepts with Requires Expressions"},"content":{"rendered":"<p>In my last post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/define-concepts\">Define Concepts<\/a>&#8220;, I defined the concepts <code>Integral<\/code>, <code>SignedIntegral<\/code>, and <code>UnsigendIntegral<\/code> using logical combinations of existing concepts and compile-time predicates. Today, I use Requires Expressions to define concepts.<\/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>Before I write about the use of requires expressions to define a concept, here is a short reminder:<\/p>\n<div>\n<div>\n<div>\n<div>The syntax to define a concept is straightforward:<\/div>\n<\/div>\n<\/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;\">template<\/span><span style=\"color: #555555;\">-<\/span>parameter<span style=\"color: #555555;\">-<\/span>list<span style=\"color: #555555;\">&gt;<\/span>\r\nconcept concept<span style=\"color: #555555;\">-<\/span>name <span style=\"color: #555555;\">=<\/span> constraint<span style=\"color: #555555;\">-<\/span>expression;\r\n<\/pre>\n<\/div>\n<div>\n<div>&nbsp;<\/div>\n<div>A concept definition starts with the keyword <code>template<\/code> and has a template parameter list. It uses the keyword <code>concept<\/code> followed by the concept name and the constraint expression.<\/div>\n<div>&nbsp;<\/div>\n<div>\n<div>\n<div>A <code>constraint-expression<\/code> is a compile-time predicate. A compile-time predicate is a function that runs at compile time and returns a boolean. This compile-time predicate can either be:<\/div>\n<p><\/p>\n<ul>\n<li>A logical combination of other concepts or compile-time predicates using conjunctions (<code>&amp;&amp;<\/code>), disjunctions (<code>||<\/code>), or negations <code>(!<\/code>). I wrote about syntactical form in my previous post t &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/define-concepts\">Define Concepts<\/a>&#8220;.<\/li>\n<\/ul>\n<p><\/p>\n<ul>\n<li>A requires expression\n<ul>\n<li>Simple requirements<\/li>\n<li>Type requirements<\/li>\n<li>Compound requirements<\/li>\n<li>Nested requirements<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Now, let&#8217;s write about requires expression.<\/p>\n<\/p>\n<h2>Requires Expression<\/h2>\n<div>\n<div>Thanks to requires expressions, you can define powerful concepts. A requires expression has the following form:<\/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%;\">requires (parameter<span style=\"color: #555555;\">-<\/span>list(optional)) {requirement<span style=\"color: #555555;\">-<\/span>seq}  \r\n<\/pre>\n<\/div>\n<p><\/p>\n<ul>\n<li><code>parameter-list:<\/code> A comma-separated list of parameters, such as in a function declaration<\/li>\n<li><code>requirement-seq:<\/code> A sequence of requirements, consisting of simple, type, compound, or nested requirements<\/li>\n<\/ul>\n<div>Requires expressions can also be used as a standalone feature when a compile-time predicate is required. I will write about this feature later.<\/div>\n<h3>Simple Requirements<\/h3>\n<div>\n<div>The following concept <code>Addable<\/code> is a simple requirement:<\/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<p><\/p>\n<div>The concept <code>Addable<\/code> requires that the addition <code>a + b<\/code> of two values of the same type <code>T<\/code> is possible.<\/div>\n<div>&nbsp;<\/div>\n<div>Before I continue with type requirements, I want to add. This concept has only one purpose: exemplifying simple requirements. Writing a concept that checks a type for supporting the + operator is bad. A concept should model an idea, such as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Arithmetic\">Arithmetic<\/a>.<\/div>\n<\/div>\n<h3>Type Requirements<\/h3>\n<div>\n<div>In a type requirement, you have to use the keyword <code>typename<\/code> together with a type name.<\/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 TypeRequirement <span style=\"color: #555555;\">=<\/span> requires {\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">::<\/span>value_type;\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Other<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;    \r\n};\r\n<\/pre>\n<\/div>\n<p><\/p>\n<div>The concept<code> TypeRequirement<\/code> requires that type <code>T<\/code> has a nested member <code>value_type<\/code>, and that the class template <code>Other<\/code> can be instantiated with<code> T<\/code>.<\/div>\n<p><\/p>\n<div>Let&#8217;s try this out:<\/div>\n<p> <!-- HTML generated using hilite.me --> <!-- 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: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&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><span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Other;  \r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Other<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&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>\r\nconcept TypeRequirement <span style=\"color: #555555;\">=<\/span> requires {\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">::<\/span>value_type;             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Other<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\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    TypeRequirement <span style=\"color: #006699; font-weight: bold;\">auto<\/span> myVec<span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p><\/p>\n<div>The expression <code>TypeRequirement auto myVec = std::vector&lt;int&gt;{1, 2, 3}<\/code> (line 1) is valid. A <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/vector\"><code>std::vector<\/code><\/a> has an inner member <code>value_type<\/code> (line 2) and the class template <code>Other<\/code> (line 2) can be instantiated with <code>std::vector&lt;int&gt;<\/code> (line 3).<\/div>\n<div>&nbsp;<\/div>\n<div>The concept <code>TypeRequirement<\/code> , in combination with <code>auto<\/code> in line 1 is called a constrained placeholder.&nbsp; In my previous post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-concepts-the-placeholder-syntax\">C++20: Concepts, the Placeholder Syntax<\/a>&#8220;, read more about constrained and unconstrained placeholders.<\/div>\n<\/div>\n<h3>Compound Requirements<\/h3>\n<div>\n<div>A compound requirement has the form<\/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%;\">{expression} noexcept(optional) <span style=\"color: #006699; font-weight: bold;\">return<\/span><span style=\"color: #555555;\">-<\/span>type<span style=\"color: #555555;\">-<\/span>requirement(optional);    \r\n<\/pre>\n<\/div>\n<p><\/p>\n<div>In addition to a simple requirement, a compound requirement can have a <code>noexcept<\/code> specifier and a requirement on its return type. You essentially express with the <code>noexcept<\/code> specifier that this expression does not throw an exception, and if it throw, you do not care and let the program just crash. Read more about the <code>noexcept<\/code> specifier in my previous post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-noexcept-specifier-and-operator\">C++ Core Guidelines: The <code>noexcept<\/code> specifier and operator<\/a>.<\/div>\n<p><\/p>\n<div>The concept <code>Equal<\/code>, demonstrated in the following example, uses compound requirements.<\/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;\">\/\/ conceptsDefinitionEqual.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;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span><\/span>\r\nconcept Equal <span style=\"color: #555555;\">=<\/span> requires(T a, T b) {\r\n    { a <span style=\"color: #555555;\">==<\/span> b } <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;\">bool<\/span><span style=\"color: #555555;\">&gt;<\/span>;\r\n    { a <span style=\"color: #555555;\">!=<\/span> b } <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;\">bool<\/span><span style=\"color: #555555;\">&gt;<\/span>;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #cc00ff;\">areEqual<\/span>(Equal <span style=\"color: #006699; font-weight: bold;\">auto<\/span> a, Equal <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\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> WithoutEqual{                                      <span style=\"color: #0099ff;\"><em> \/\/ (2)<\/em><\/span>\r\n  <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">==<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> WithoutEqual<span style=\"color: #555555;\">&amp;<\/span> other) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> WithoutUnequal{                                    <span style=\"color: #0099ff;\"><em> \/\/ (3)<\/em><\/span>\r\n  <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">!=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> WithoutUnequal<span style=\"color: #555555;\">&amp;<\/span> other) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\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> std<span style=\"color: #555555;\">::<\/span>boolalpha <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;\">\"areEqual(1, 5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> areEqual(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/*<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\"> <\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">    bool res = areEqual(WithoutEqual(),  WithoutEqual());    \/\/ (4)<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">    bool res2 = areEqual(WithoutUnequal(),  WithoutUnequal());<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\"> <\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">    *\/<\/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><\/p>\n<div>The concept <code>Equal<\/code> (line 1) requires that its type parameter<code> T<\/code> supports the equal and non-equal operators. Additionally, both operators have to return a convertible value to a boolean. Of course, <code>int<\/code> supports the concept <code>Equal<\/code>, but this does not hold for the types<code> WithoutEqual<\/code> (line 2) and<code> WithoutUnequal<\/code> (line 3). Consequently, when I use the type <code>WithoutEqual<\/code> (line 4), I get the following error message when using the GCC compiler.<\/div>\n<p><\/p>\n<div><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6346\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/equalError.png\" alt=\"equalError\" width=\"500\" height=\"118\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/equalError.png 899w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/equalError-300x71.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/04\/equalError-768x182.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/div>\n<\/div>\n<h3>Nested Requirements<\/h3>\n<div>\n<div>A nested requirement has the form<\/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%;\">requires constraint<span style=\"color: #555555;\">-<\/span>expression;   \r\n<\/pre>\n<\/div>\n<p><\/p>\n<div>Nested requirements are used to specify requirements on type parameters.<\/div>\n<p><\/p>\n<div>In my previous post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/define-concepts\">Define Concepts<\/a>&#8220;, I defined the concept<code> <\/code><code>UnsignedIntegral<\/code> using logical combinations of existing concepts and compile-time predicates. Now, I define it using nested requirements:<\/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;\">\/\/ nestedRequirements.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;type_traits&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>\r\nconcept Integral <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>is_integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value;\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\nconcept SignedIntegral <span style=\"color: #555555;\">=<\/span> Integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> std<span style=\"color: #555555;\">::<\/span>is_signed<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value;\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ template &lt;typename T&gt;                               \/\/ (2)<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ concept UnsignedIntegral = Integral&lt;T&gt; &amp;&amp; !SignedIntegral&lt;T&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 style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span><\/span>\r\nconcept UnsignedIntegral <span style=\"color: #555555;\">=<\/span> Integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span>\r\nrequires(T) {\r\n    requires <span style=\"color: #555555;\">!<\/span>SignedIntegral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;\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    UnsignedIntegral <span style=\"color: #006699; font-weight: bold;\">auto<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5u<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ works<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ UnsignedIntegral auto m = 5;   \/\/ compile time error, 5 is a signed literal<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p><\/p>\n<div>Line (1) uses the concept <code>SignedIntegral<\/code> as a nested requirement to refine the concept <code>Integral<\/code>. The commented-out concept UnsignedIntegral in line (2) is more convenient to read.<\/div>\n<h2>What&#8217;s next?<\/h2>\n<div>Typically, you use requires expressions to define a concept, but they can also be used as a standalone feature when a compile-time predicate is required. Therefore, use cases for requires expression can be a requires clause, <code>static_assert<\/code>or <code>constexpr if<\/code>. I will write in my next post about these unique use cases of requires expressions.<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my last post, &#8220;Define Concepts&#8220;, I defined the concepts Integral, SignedIntegral, and UnsigendIntegral using logical combinations of existing concepts and compile-time predicates. Today, I use Requires Expressions to define concepts.<\/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,419],"class_list":["post-6347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-concepts","tag-type-traits"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6347","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=6347"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6347\/revisions"}],"predecessor-version":[{"id":6672,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6347\/revisions\/6672"}],"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=6347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}