Skip to content

Commit 24fa725

Browse files
authored
Requires Expressions (#58)
Adds initial version of teaching topic for requires expressions
1 parent c47611c commit 24fa725

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

compile-time-programming/concepts.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Compile-time programming: concepts
2+
3+
This topic is currently under construction and will soon be filled with information :)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Compile-time programming: Function Templates
2+
3+
This topic is currently under construction and will soon be filled with information :)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Compile-time programming: requires-clause
2+
3+
This topic is currently under construction and will soon be filled with information :)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Module name: Requires Expressions
2+
3+
## Overview
4+
5+
<table>
6+
<thead>
7+
<th>Level</th>
8+
<th>Objectives</th>
9+
</thead>
10+
<tr>
11+
<td>Foundational</td>
12+
<td>Define and use requires-expressions to check satisfaction of expressions by given parameters</td>
13+
</tr>
14+
<tr>
15+
<td>Main</td>
16+
<td>Define and use requires-expressions to check properties of expressions</td>
17+
</tr>
18+
<tr>
19+
<td>Advanced</td>
20+
<td></td>
21+
</tr>
22+
</table>
23+
24+
## Motivation
25+
26+
Requires-expressions allow a developer to perform compile-time evaluation
27+
on the validity of other expressions. These are fundamental to the ability
28+
to write concepts. [[Compile-time programming: concepts]][1]
29+
30+
## Topic introduction
31+
32+
Requires-expressions are compile-time predicates which evaluate to true
33+
when their specified set of expressions are all valid for a given set of
34+
inputs.
35+
36+
## Foundational: Writing requires-expressions
37+
38+
### Background/Required Knowledge
39+
40+
A student is able to:
41+
42+
* Write and use a function template [[Compile-time programming: function templates]][2]
43+
* Differentiate between expressions and statements
44+
45+
It is helpful if:
46+
47+
* The student is aware that attempting to specialize the template with types or values which do not match otherwise unstated assumptions will cause errors within the template.
48+
49+
### Student outcomes
50+
51+
A student should be able to:
52+
53+
1. Write a simple-requirement to assert the validity of an expression
54+
2. Write a type-requirement to check the existence of a type by its identifier
55+
3. Write a compound-requirement to test the resulting type of an expression
56+
4. Write a nested-requirement to test the constexpr value of an operation, as opposed to just the syntactic validity
57+
5. Use a requires-expression within a concept, requires-clause, or `if constexpr` condition
58+
59+
### Caveats
60+
61+
To require that expressions, which evaluate to a boolean value
62+
like `sizeof(t) == 4`, evaluate to `true` a nested-requirement is needed
63+
(e.g., `requires sizeof(t) == 4;`). Omitting the `requires` results in a
64+
simple-requirement, which is satisfied based purely on syntactic validity,
65+
not on the result of the operation.
66+
67+
### Points to cover
68+
69+
* All requires-expression requirements terminate with a semicolon.
70+
* simple-requirements are used to check that an expression is well-formed.
71+
* nested-requirements are introduced with `requires` and primarily used to check the result of an expression computable by the compiler, including concepts or other requires-expressions.
72+
* type-requirements are introduced with `typename` and used to verify the existence of a type with a particular identifier.
73+
* compound-requirements are enclosed in braces and can be used to check the resulting type of an expression.
74+
* Checks are performed by the compiler, not at run time.
75+
* If covering usage of requires-expression with requires-clause, [[Compile-time programming: requires clause]][3] demonstrate `requires requires` and show how to ever avoid writing it by using a concept. [[Compile-time programming: concepts]][1]
76+
77+
## Main: Advanced requirements
78+
79+
### Background/required knowledge
80+
81+
* All of the above.
82+
* Knowledge of `noexcept`
83+
84+
A student is able to:
85+
86+
* Write a concept [[Compile-time programming: concepts]][1]
87+
88+
### Student outcomes
89+
90+
A student should be able to:
91+
92+
1. Write compound-requirements which test the `noexcept`ness of an expression.
93+
2. Use a concept as the target of a compound-requirement.
94+
95+
### Caveats
96+
97+
### Points to cover
98+
99+
* Compound-requirements allow the optional ability to test whether an expression is marked as `noexcept`, by using a trailing `noexcept` keyword.
100+
```
101+
struct S
102+
{
103+
void foo() noexcept {}
104+
void bar() {}
105+
};
106+
107+
static_assert(requires(S s) { { s.foo() } noexcept; } ); // Succeeds. s.foo() is noexcept
108+
static_assert(requires(S s) { { s.bar() } noexcept; } ); // Fails. s.bar() is not noexcept
109+
```
110+
* If the return-type-requirement of a compound-requirement is a concept, that concept is given the resulting type as the first parameter, followed by the specified parameters in the compound-requirement. `{ ++x } -> C<int>` would substitute `C<decltype((++x)), int>` and check that concept C is satisfied for those parameters.
111+
112+
## Advanced
113+
114+
[1]: ../compile-time-programming/concepts.md
115+
[2]: ../compile-time-programming/function-templates.md
116+
[3]: ../compile-time-programming/requires-clause.md

program-design/concepts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See [concepts](../compile-time-programmings/concepts.md).

0 commit comments

Comments
 (0)