|
| 1 | +# Functions: user-defined literals |
| 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>using and understanding UDLs</td> |
| 17 | + </tr> |
| 18 | + <tr> |
| 19 | + <td>Main</td> |
| 20 | + <td>implementing your own UDLs</td> |
| 21 | + </tr> |
| 22 | + <tr> |
| 23 | + <td>Advanced</td> |
| 24 | + <td>Advanced use (<code>"{}, {}!"_fmt("Hello", "World")</code>)</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 | +* Allows clearer expression of intent in C++. |
| 34 | +* `std::string`: `"Hello, world!"s` |
| 35 | +* `std::chrono`: `3h + 10m + 5s` |
| 36 | + |
| 37 | +## Topic introduction |
| 38 | + |
| 39 | +_Very brief introduction to the topic._ |
| 40 | + |
| 41 | +* Explain the existence of user defined literals. Example: `12m + 17s` is terse, |
| 42 | + expressive and type safe. |
| 43 | + |
| 44 | +## Foundational: Using UDLs |
| 45 | + |
| 46 | +### Background/Required Knowledge |
| 47 | + |
| 48 | +A student: |
| 49 | +* knows how to form numeric literals, e.g., `1.5f` means a `float` of value `1.5`. |
| 50 | +* is familiar with the major C++ types: |
| 51 | + * `bool` (Boolean type) |
| 52 | + * `int` (Integer type) |
| 53 | + * `double` (Floating-point type) |
| 54 | + * `std::string` (Text type) |
| 55 | + * `std::vector` (Collection type) |
| 56 | +* knows that namespaces exist, and namespace `std`. |
| 57 | +* knows what using-declarations and using-directives are. [[C++ object model: declarations]][2] |
| 58 | + |
| 59 | +### Student outcomes |
| 60 | + |
| 61 | +_A list of things “a student should be able to” after the curriculum._ |
| 62 | +_The next word should be an action word and testable in an exam._ |
| 63 | +_Max 5 items._ |
| 64 | + |
| 65 | +A student should be able to: |
| 66 | + |
| 67 | +1. use `using namespace std::string_literals`\[1]. |
| 68 | +2. recognise UDLs in code that they are reading. |
| 69 | +3. figure out which UDL definitions for a used type exist. |
| 70 | +4. identify parts of the standard library that make use of UDLs. |
| 71 | +5. prevent the dangers of temporaries created with `"blah"s` as well as with |
| 72 | + `std::string{"blah"}`. |
| 73 | +6. effectively selects the right set of namespaces in using-directives from the |
| 74 | + sub-namespaces `std::literals`. |
| 75 | + |
| 76 | +\[1]: explain that it's okay to use a using-directive to "activate" UDLs. |
| 77 | + |
| 78 | +### Caveats |
| 79 | + |
| 80 | +_This section mentions subtle points to understand, like anything resulting in |
| 81 | +implementation-defined, unspecified, or undefined behavior._ |
| 82 | + |
| 83 | +* A student gets confused by the similarities and differences between built-in |
| 84 | + suffixes and UDLs and between UDLs from different namespaces. |
| 85 | +* A student "activates" two suffixes with the same signature from different |
| 86 | + namespaces. |
| 87 | + |
| 88 | +### Points to cover |
| 89 | + |
| 90 | +_This section lists important details for each point._ |
| 91 | + |
| 92 | +## Main: implementing UDLs |
| 93 | + |
| 94 | +### Background/required knowledge |
| 95 | + |
| 96 | +* All of the above. |
| 97 | + |
| 98 | +### Student outcomes |
| 99 | + |
| 100 | +A student should be able to: |
| 101 | + |
| 102 | +1. write a UDL operator of their own. |
| 103 | +2. separate unrelated UDLs into distinct namespaces. |
| 104 | + |
| 105 | +### Caveats |
| 106 | +No caveats at present. |
| 107 | +### Points to cover |
| 108 | +No caveats at present. |
| 109 | +## Advanced |
| 110 | + |
| 111 | +_These are important topics that are not expected to be covered but provide |
| 112 | +guidance where one can continue to investigate this topic in more depth._ |
| 113 | + |
| 114 | +[1]: ../object-model/types.md |
| 115 | +[2]: ../object-model/declarations.md |
0 commit comments