diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2026-08-30 18:35:23 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2026-08-30 18:35:23 +0200 |
| commit | 5e3b4b6c21d33b28e60ce976479339f72d544a6c (patch) | |
| tree | 0e5a101d4944e7dd5a99606344b6a6807a9a5606 /src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md | |
| parent | d3f0841cfdd229ff16b8a230117737a435fc7a56 (diff) | |
| download | sebastiano.tronto.net-5e3b4b6c21d33b28e60ce976479339f72d544a6c.tar.gz sebastiano.tronto.net-5e3b4b6c21d33b28e60ce976479339f72d544a6c.zip | |
Switch from stagit to cgit
Diffstat (limited to 'src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md')
| -rw-r--r-- | src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md b/src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md index bce440f..903a1ff 100644 --- a/src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md +++ b/src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md | |||
| @@ -44,7 +44,7 @@ You can find the code examples for this post in the | |||
| 44 | [companion repository for this series](https://git.tronto.net/taming-cpp), | 44 | [companion repository for this series](https://git.tronto.net/taming-cpp), |
| 45 | and if you want you can also have a look at the final version | 45 | and if you want you can also have a look at the final version |
| 46 | of my tiny library | 46 | of my tiny library |
| 47 | [on my git page](https://git.tronto.net/zmodn/file/README.md.html). For | 47 | [on my git page](https://git.tronto.net/zmodn). For |
| 48 | this post I am going to use Clang as a compiler, because I find its | 48 | this post I am going to use Clang as a compiler, because I find its |
| 49 | error messages more readable most of the times. | 49 | error messages more readable most of the times. |
| 50 | 50 | ||
| @@ -69,7 +69,7 @@ cannot, for example, assign an `std::vector<int>` object to an | |||
| 69 | As an example, let's take a simple standard library class such as | 69 | As an example, let's take a simple standard library class such as |
| 70 | [`std::pair`](https://en.cppreference.com/w/cpp/utility/pair). | 70 | [`std::pair`](https://en.cppreference.com/w/cpp/utility/pair). |
| 71 | It could be implemented as follows (see | 71 | It could be implemented as follows (see |
| 72 | [pair.cpp](https://git.tronto.net/taming-cpp/file/templates/pair.cpp.html)): | 72 | [pair.cpp](https://git.tronto.net/taming-cpp/tree/templates/pair.cpp)): |
| 73 | 73 | ||
| 74 | ``` | 74 | ``` |
| 75 | template<typename S, typename T> | 75 | template<typename S, typename T> |
| @@ -151,7 +151,7 @@ which in case you don't know are just a simplified syntax for pointers.) | |||
| 151 | Let's take this simple example to show an important property of templates. | 151 | Let's take this simple example to show an important property of templates. |
| 152 | Let's say that, perhaps by mistake, we implemented the swap function | 152 | Let's say that, perhaps by mistake, we implemented the swap function |
| 153 | using different template types for `a` and `b` (see | 153 | using different template types for `a` and `b` (see |
| 154 | [swap.cpp](https://git.tronto.net/taming-cpp/file/templates/swap.cpp.html)): | 154 | [swap.cpp](https://git.tronto.net/taming-cpp/tree/templates/swap.cpp)): |
| 155 | 155 | ||
| 156 | ``` | 156 | ``` |
| 157 | template<typename S, typename T> | 157 | template<typename S, typename T> |
| @@ -204,7 +204,7 @@ to make these error messages more meaningful - we'll see some examples below. | |||
| 204 | Objects, not just types, can be template parameters. A classic example | 204 | Objects, not just types, can be template parameters. A classic example |
| 205 | is [`std::array`](https://en.cppreference.com/w/cpp/container/array), | 205 | is [`std::array`](https://en.cppreference.com/w/cpp/container/array), |
| 206 | a fixed-size container where the capacity is fixed at compile time (see | 206 | a fixed-size container where the capacity is fixed at compile time (see |
| 207 | [std_array.cpp](https://git.tronto.net/taming-cpp/file/templates/std_array.cpp.html) | 207 | [std_array.cpp](https://git.tronto.net/taming-cpp/tree/templates/std_array.cpp) |
| 208 | for an example). | 208 | for an example). |
| 209 | 209 | ||
| 210 | Non-type parameters can be constants of any *structural type* - see | 210 | Non-type parameters can be constants of any *structural type* - see |
| @@ -213,13 +213,13 @@ for a precise definition. Remember that you can only specialize them | |||
| 213 | with compile-time (i.e. `constexpr`) constants! | 213 | with compile-time (i.e. `constexpr`) constants! |
| 214 | 214 | ||
| 215 | With non-type parameter you can do pretty wild stuff, see for example | 215 | With non-type parameter you can do pretty wild stuff, see for example |
| 216 | [factorial.cpp](https://git.tronto.net/taming-cpp/file/templates/factorial.cpp.html) | 216 | [factorial.cpp](https://git.tronto.net/taming-cpp/tree/templates/factorial.cpp) |
| 217 | - although this specific example is not very useful, since it can easily | 217 | - although this specific example is not very useful, since it can easily |
| 218 | be replaced by a constexpr function. | 218 | be replaced by a constexpr function. |
| 219 | 219 | ||
| 220 | Fun fact: if you use `auto`, you don't even have to specify a type for | 220 | Fun fact: if you use `auto`, you don't even have to specify a type for |
| 221 | a non-type parameter. For example, the following code works just fine (see | 221 | a non-type parameter. For example, the following code works just fine (see |
| 222 | [println.cpp](https://git.tronto.net/taming-cpp/file/templates/println.cpp.html)): | 222 | [println.cpp](https://git.tronto.net/taming-cpp/tree/templates/println.cpp)): |
| 223 | 223 | ||
| 224 | ``` | 224 | ``` |
| 225 | #include <iostream> | 225 | #include <iostream> |
| @@ -278,7 +278,7 @@ template for | |||
| 278 | where `N` is a fixed at compile-time. | 278 | where `N` is a fixed at compile-time. |
| 279 | 279 | ||
| 280 | We may start with something like this (see | 280 | We may start with something like this (see |
| 281 | [zmodn-1.cpp](https://git.tronto.net/taming-cpp/file/templates/zmodn-1.cpp.html)): | 281 | [zmodn-1.cpp](https://git.tronto.net/taming-cpp/tree/templates/zmodn-1.cpp)): |
| 282 | 282 | ||
| 283 | ``` | 283 | ``` |
| 284 | #include <iostream> | 284 | #include <iostream> |
| @@ -355,7 +355,7 @@ are a way to prevent nasty run-time errors and / or make compiler errors | |||
| 355 | more meaningful when using templates; they were added in C++20. | 355 | more meaningful when using templates; they were added in C++20. |
| 356 | 356 | ||
| 357 | In our case, introducing our constraint is quite simple (see | 357 | In our case, introducing our constraint is quite simple (see |
| 358 | [zmodn-2.cpp](https://git.tronto.net/taming-cpp/file/templates/zmodn-2.cpp.html)): | 358 | [zmodn-2.cpp](https://git.tronto.net/taming-cpp/tree/templates/zmodn-2.cpp)): |
| 359 | 359 | ||
| 360 | ``` | 360 | ``` |
| 361 | template<int N> | 361 | template<int N> |
| @@ -392,7 +392,7 @@ type of N. | |||
| 392 | 392 | ||
| 393 | In order to do so, I can use a non-type parameter declared `auto` and | 393 | In order to do so, I can use a non-type parameter declared `auto` and |
| 394 | [`decltype()`](https://en.cppreference.com/w/cpp/language/decltype) (see | 394 | [`decltype()`](https://en.cppreference.com/w/cpp/language/decltype) (see |
| 395 | [zmodn-3.cpp](https://git.tronto.net/taming-cpp/file/templates/zmodn-3.cpp.html)): | 395 | [zmodn-3.cpp](https://git.tronto.net/taming-cpp/tree/templates/zmodn-3.cpp)): |
| 396 | 396 | ||
| 397 | ``` | 397 | ``` |
| 398 | template<auto N> | 398 | template<auto N> |
| @@ -409,7 +409,7 @@ public: | |||
| 409 | 409 | ||
| 410 | And of course I should also templatize the `extended_gcd()` function - | 410 | And of course I should also templatize the `extended_gcd()` function - |
| 411 | you can see the full code in | 411 | you can see the full code in |
| 412 | [zmodn-3.cpp](https://git.tronto.net/taming-cpp/file/templates/zmodn-3.cpp.html). | 412 | [zmodn-3.cpp](https://git.tronto.net/taming-cpp/tree/templates/zmodn-3.cpp). |
| 413 | 413 | ||
| 414 | Now we can use any type as a "base" for our modular integer! Well, almost. | 414 | Now we can use any type as a "base" for our modular integer! Well, almost. |
| 415 | I mentioned above that the type we use must be *structural*, but that is | 415 | I mentioned above that the type we use must be *structural*, but that is |
| @@ -417,7 +417,7 @@ relatively easy to satisfy. A bigger problem is that our type must | |||
| 417 | allow for compile-time constants - so we need, at least, a `constexpr` | 417 | allow for compile-time constants - so we need, at least, a `constexpr` |
| 418 | constructor. I could not find a suitable library online, so I ended | 418 | constructor. I could not find a suitable library online, so I ended |
| 419 | up writing my own - see | 419 | up writing my own - see |
| 420 | [bigint.h](https://git.tronto.net/taming-cpp/file/templates/bigint.h.html). | 420 | [bigint.h](https://git.tronto.net/taming-cpp/tree/templates/bigint.h). |
| 421 | 421 | ||
| 422 | The code is simple and not very efficient, but this library is not meant | 422 | The code is simple and not very efficient, but this library is not meant |
| 423 | to be efficient. I am just using it for educational purposes. | 423 | to be efficient. I am just using it for educational purposes. |
| @@ -477,7 +477,7 @@ Along with constraints, C++20 also introduced the possibility to define | |||
| 477 | and name a custom set of requirements. This can be done with *concepts*. | 477 | and name a custom set of requirements. This can be done with *concepts*. |
| 478 | In our example, we can require that our type supports all the operations | 478 | In our example, we can require that our type supports all the operations |
| 479 | we need (see | 479 | we need (see |
| 480 | [zmodn-4.cpp](https://git.tronto.net/taming-cpp/file/templates/zmodn-4.cpp.html)): | 480 | [zmodn-4.cpp](https://git.tronto.net/taming-cpp/tree/templates/zmodn-4.cpp)): |
| 481 | 481 | ||
| 482 | ``` | 482 | ``` |
| 483 | template<typename T> | 483 | template<typename T> |
