Skip to content

Commit e657d2f

Browse files
authored
Straw-man for static_assert (#62)
resolves #19
1 parent 24873f1 commit e657d2f

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

meta-error-handling/static_assert.md

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Meta-error handling: `static_assert`
2+
_Skeleton instructions are typeset in italic text._
3+
4+
## Overview
5+
6+
_Provides a short natural language abstract of the module’s contents._
7+
_Specifies the different levels of teaching._
8+
9+
<table>
10+
<thead>
11+
<th>Level</th>
12+
<th>Objectives</th>
13+
</thead>
14+
<tr>
15+
<td>Foundational</td>
16+
<td>Calling <code>static_assert</code> with a constant expression</td>
17+
</tr>
18+
<tr>
19+
<td>Main</td>
20+
<td>Using <code>static_assert</code> to detect contract violations and improve error messages</td>
21+
</tr>
22+
<tr>
23+
<td>Advanced</td>
24+
<td></td>
25+
</tr>
26+
</table>
27+
28+
## Motivation
29+
30+
_Why is this important?_
31+
_Why do we want to learn/teach this topic?_
32+
33+
`static_assert` allows the developer to enforce that conditions which can
34+
be checked during compilation will force build errors when violated.
35+
Additionally, they are the best mechanism by which a developer can pass
36+
useful information to other developers regarding what violation occured or
37+
what must be done, instead.
38+
39+
## Topic introduction
40+
41+
_Very brief introduction to the topic._
42+
43+
`static_assert` is a compile-time evaluated function that asserts the
44+
truth of a supplied predicate, issuing an optional user-supplied error
45+
message if the predicate is `false`.
46+
47+
## Foundational: Calling `static_assert` with a constant expression
48+
49+
### Background/Required Knowledge
50+
51+
A student:
52+
53+
* Should be able to explain the difference between code evaluated at compile-time and run-time
54+
* Should be able to cite some examples of compile-time known information, such as `sizeof(T)`
55+
56+
### Student outcomes
57+
58+
_A list of things "a student should be able to" after the curriculum._
59+
_The next word should be an action word and testable in an exam._
60+
_Max 5 items._
61+
62+
A student should be able to:
63+
64+
1. Assert the expected size of a structure using `static_assert`
65+
66+
### Caveats
67+
68+
_This section mentions subtle points to understand, like anything resulting in
69+
implementation-defined, unspecified, or undefined behavior._
70+
71+
### Points to cover
72+
73+
_This section lists important details for each point._
74+
75+
* X
76+
* In addition to what is wrong, a good error message will inform the user of how to correct it
77+
78+
## Main: Contracts and `static_assert`
79+
80+
### Background/Required Knowledge
81+
82+
* All of the above.
83+
* General understanding of compile-time requirements
84+
85+
### Student outcomes
86+
87+
_A list of things "a student should be able to" after the curriculum._
88+
_The next word should be an action word and testable in an exam._
89+
_Max 5 items._
90+
91+
A student should be able to:
92+
93+
1. Utilize `static_assert` to verify pre-conditions of a meta-function
94+
2. Utilize `static_assert` to verify the results of meta-functions for known values
95+
96+
### Caveats
97+
98+
_This section mentions subtle points to understand, like anything resulting in
99+
implementation-defined, unspecified, or undefined behavior._
100+
101+
### Points to cover
102+
103+
_This section lists important details for each point._
104+
105+
* When writing a meta-function, use `static_assert` to test the results
106+
* Write `static_assert` calls at the scope of the code they are guarding
107+
```cpp
108+
template<typename T>
109+
struct container {
110+
std::map<int, T> vals;
111+
112+
// Test location #1
113+
static_assert(
114+
std::is_default_constructible_v<T>,
115+
"container type T must be default constructible");i
116+
117+
void add(int key, T const& t) {
118+
// Test location #2
119+
static_assert(
120+
std::is_default_constructible_v<T>,
121+
"container type T must be default constructible");
122+
// std::map::operator[] requires default constructible type for
123+
// the value. This will cause a build failure deep in the
124+
// implementation of std::map, when T is not default constructible
125+
vals[key] = t;
126+
}
127+
};
128+
129+
struct NoDefCtor {
130+
NoDefCtor() = delete;
131+
NoDefCtor(double d) {}
132+
};
133+
134+
container<NoDefCtor> c; // If Test #1 was omitted, this would succeed
135+
// This is ill-formed. Test #2 would catch this and provide a better
136+
// error message for the user
137+
c.add(42, NoDefCtor(1.0));
138+
```
139+
140+
## Advanced
141+
142+
_These are important topics that are not expected to be covered but provide
143+
guidance where one can continue to investigate this topic in more depth._

0 commit comments

Comments
 (0)