aboutsummaryrefslogtreecommitdiff
path: root/src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-08-30 18:35:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-08-30 18:35:23 +0200
commit5e3b4b6c21d33b28e60ce976479339f72d544a6c (patch)
tree0e5a101d4944e7dd5a99606344b6a6807a9a5606 /src/blog/2025-01-21-taming-cpp-templates/taming-cpp-templates.md
parentd3f0841cfdd229ff16b8a230117737a435fc7a56 (diff)
downloadsebastiano.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.md24
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),
45and if you want you can also have a look at the final version 45and if you want you can also have a look at the final version
46of my tiny library 46of 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
48this post I am going to use Clang as a compiler, because I find its 48this post I am going to use Clang as a compiler, because I find its
49error messages more readable most of the times. 49error messages more readable most of the times.
50 50
@@ -69,7 +69,7 @@ cannot, for example, assign an `std::vector<int>` object to an
69As an example, let's take a simple standard library class such as 69As 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).
71It could be implemented as follows (see 71It 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```
75template<typename S, typename T> 75template<typename S, typename T>
@@ -151,7 +151,7 @@ which in case you don't know are just a simplified syntax for pointers.)
151Let's take this simple example to show an important property of templates. 151Let's take this simple example to show an important property of templates.
152Let's say that, perhaps by mistake, we implemented the swap function 152Let's say that, perhaps by mistake, we implemented the swap function
153using different template types for `a` and `b` (see 153using 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```
157template<typename S, typename T> 157template<typename S, typename T>
@@ -204,7 +204,7 @@ to make these error messages more meaningful - we'll see some examples below.
204Objects, not just types, can be template parameters. A classic example 204Objects, not just types, can be template parameters. A classic example
205is [`std::array`](https://en.cppreference.com/w/cpp/container/array), 205is [`std::array`](https://en.cppreference.com/w/cpp/container/array),
206a fixed-size container where the capacity is fixed at compile time (see 206a 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)
208for an example). 208for an example).
209 209
210Non-type parameters can be constants of any *structural type* - see 210Non-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
213with compile-time (i.e. `constexpr`) constants! 213with compile-time (i.e. `constexpr`) constants!
214 214
215With non-type parameter you can do pretty wild stuff, see for example 215With 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
218be replaced by a constexpr function. 218be replaced by a constexpr function.
219 219
220Fun fact: if you use `auto`, you don't even have to specify a type for 220Fun fact: if you use `auto`, you don't even have to specify a type for
221a non-type parameter. For example, the following code works just fine (see 221a 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
278where `N` is a fixed at compile-time. 278where `N` is a fixed at compile-time.
279 279
280We may start with something like this (see 280We 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
355more meaningful when using templates; they were added in C++20. 355more meaningful when using templates; they were added in C++20.
356 356
357In our case, introducing our constraint is quite simple (see 357In 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```
361template<int N> 361template<int N>
@@ -392,7 +392,7 @@ type of N.
392 392
393In order to do so, I can use a non-type parameter declared `auto` and 393In 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```
398template<auto N> 398template<auto N>
@@ -409,7 +409,7 @@ public:
409 409
410And of course I should also templatize the `extended_gcd()` function - 410And of course I should also templatize the `extended_gcd()` function -
411you can see the full code in 411you 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
414Now we can use any type as a "base" for our modular integer! Well, almost. 414Now we can use any type as a "base" for our modular integer! Well, almost.
415I mentioned above that the type we use must be *structural*, but that is 415I 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
417allow for compile-time constants - so we need, at least, a `constexpr` 417allow for compile-time constants - so we need, at least, a `constexpr`
418constructor. I could not find a suitable library online, so I ended 418constructor. I could not find a suitable library online, so I ended
419up writing my own - see 419up 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
422The code is simple and not very efficient, but this library is not meant 422The code is simple and not very efficient, but this library is not meant
423to be efficient. I am just using it for educational purposes. 423to 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
477and name a custom set of requirements. This can be done with *concepts*. 477and name a custom set of requirements. This can be done with *concepts*.
478In our example, we can require that our type supports all the operations 478In our example, we can require that our type supports all the operations
479we need (see 479we 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```
483template<typename T> 483template<typename T>

Generated with cgit - Back to sebastiano.tronto.net