Skip to content

Commit e74134d

Browse files
committed
Adds initial draft for concepts topic
1 parent 80780c5 commit e74134d

File tree

4 files changed

+131
-7
lines changed

4 files changed

+131
-7
lines changed
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
## Compile-time programming: concepts
2-
3-
_Skeleton descriptions are typeset in italic text,_
4-
_so please don't remove these descriptions when editing the topic._
5-
6-
This topic is currently under construction and will soon be filled with information :)
1+
See [concepts](../program-design/concepts.md).
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Functions:: Overloading
2+
3+
_Skeleton descriptions are typeset in italic text,_
4+
_so please don't remove these descriptions when editing the topic._
5+
6+
This topic is currently under construction and will soon be filled with information :)
Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,124 @@
1-
See [concepts](../compile-time-programmings/concepts.md).
1+
## Program Design: Concepts {#concepts}
2+
3+
_Skeleton descriptions are typeset in italic text,_
4+
_so please don't remove these descriptions when editing the topic._
5+
6+
### Overview
7+
8+
_Provides a short natural language abstract of the module’s contents._
9+
_Specifies the different levels of teaching._
10+
11+
------------------------------------------------------------------------
12+
Level Objective
13+
----------------- ------------------------------------------------------
14+
Foundational Using constraints
15+
16+
Main Designing concepts
17+
18+
Advanced Testing concepts
19+
20+
------------------------------------------------------------------------
21+
22+
### Motivation
23+
24+
_Why is this important?_
25+
_Why do we want to learn/teach this topic?_
26+
27+
When writing generic code it is important to specify what set of types are supported, to prevent users from misusing it. That is, we want to prevent generic code from compiling for types that are not supported. Furthermore, we also then want to return useful error messages that describe why a type was not usable.
28+
29+
Concepts allow us to provide different specialized implementations for a function or class. These implementations can then make use of specific characteristics of the chosen subset of the allowed types without relying on hacky tricks. Overall, concepts allow us to write better documenting and more expressive code, making it easier to write, read, and maintain generic code.
30+
31+
32+
### Topic introduction
33+
34+
_Very brief introduction to the topic._
35+
36+
### Foundational: Using constraints {#concepts-found}
37+
38+
#### Background/Required Knowledge
39+
40+
A student:
41+
42+
* [[Functions: Function Templates - Foundational]][1]
43+
* [[User-defined Types: Class Templates - Foundational]][2]
44+
45+
#### Student outcomes
46+
47+
_A list of things "a student should be able to" after the curriculum._
48+
_The next word should be an action word and testable in an exam._
49+
_Max 5 items._
50+
51+
A student should be able to:
52+
53+
1. write code that calls a concept function
54+
2. write a simple template that is constrained
55+
3. write code that composes multiple constraints
56+
4. enumerate some standard defined concepts
57+
58+
59+
#### Caveats
60+
61+
_This section mentions subtle points to understand, like anything resulting in
62+
implementation-defined, unspecified, or undefined behavior._
63+
64+
* There used to be workarounds that were used to constrain interfaces (e.g., partial specialization, SFINAE) but they were cumbersome and hard to debug. In modern C++ `concepts` should be used instead.
65+
66+
#### Points to cover
67+
68+
_This section lists important details for each point._
69+
70+
* Simple example where we need constraints (e.g., min/max, sqrt, printing, …)
71+
* Why constraints at all:
72+
* Can express conceptual design directly
73+
* get better compile time checking with good error messages
74+
* can add constraints that are not in the type system
75+
* unconstrained templates are too generic, constraints allow us to restrict what types are applicable for a template
76+
* make important properties of an algorithm part of the type interface (e.g., that a collection needs to be sortable)
77+
* Constraints can be composed with && and ||
78+
* The standard has some named constraints, i.e., `concepts`
79+
80+
81+
### Main: Designing concepts {#concepts-main}
82+
83+
#### Background/Required Knowledge
84+
85+
* [Optional] Overload resolution [[Functions: Overloading]][3]
86+
* [Optional] Template specializations [[Compile-time programming: Template Meta Programming]][4]
87+
88+
#### Student outcomes
89+
90+
A student should be able to:
91+
92+
* make an educated decision on whether to create a new concept or use preexisting one (e.g., from the standard)
93+
* design a new concept following best practices
94+
* use concepts to build overload sets/specialize templates [Optional]
95+
96+
97+
#### Caveats
98+
99+
* further constraining previously published concepts can break user code
100+
* `requires requires`: consider that a single requires only checks if an expression is syntactically correct and that `requires requires` allows one to check if the logical value on an expression is true.
101+
102+
#### Points to cover
103+
104+
* Design rules for concepts
105+
* first, check if the desired set of constraints already exists in a standard provided concept
106+
* prefer refining concepts over expressing all constraints directly
107+
* try to not further constrain previously published concepts, as this can break user code
108+
* consider that under-specified constraints can be too generic but also that over-constraining a template limits its applicability (i.e., select only the constraints that are important for a template to work in a generic context)
109+
* [Optional] if a more specialized implementation is possible for a constrained group of types (i.e., given the constraint from a concept we have strong guarantees for the implementation), use "overloading"/template specialization on concepts to select the specialized implementations (e.g., std::advance())
110+
* most constrained template is chosen
111+
* prefer concepts over “SFINAE” when constraining overload sets
112+
113+
114+
### Advanced
115+
116+
_These are important topics that are not expected to be covered but provide
117+
guidance where one can continue to investigate this topic in more depth._
118+
119+
* Writing tests for concepts to ensure they specify exactly what was intended.
120+
121+
[1]: ../functions/function-templates.md
122+
[2]: ../user-defined-types/class-templates.md
123+
[3]: ../functions/overloading.md
124+
[4]: ../compile-time-programming/template-meta-programming.md

0 commit comments

Comments
 (0)