aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--templates/factorial.cpp22
-rw-r--r--templates/println.cpp18
-rw-r--r--templates/std_array.cpp16
3 files changed, 56 insertions, 0 deletions
diff --git a/templates/factorial.cpp b/templates/factorial.cpp
new file mode 100644
index 0000000..11e4789
--- /dev/null
+++ b/templates/factorial.cpp
@@ -0,0 +1,22 @@
1// Taken from https://en.wikipedia.org/wiki/Template_metaprogramming
2
3#include <iostream>
4
5template<long long N>
6class factorial {
7public:
8 static constexpr long long value = N * factorial<N-1>::value;
9};
10
11template<>
12class factorial<0> {
13public:
14 static constexpr long long value = 1;
15};
16
17int main() {
18 constexpr long long X = 12;
19 std::cout << factorial<X>::value << std::endl;
20
21 return 0;
22}
diff --git a/templates/println.cpp b/templates/println.cpp
new file mode 100644
index 0000000..62f27ac
--- /dev/null
+++ b/templates/println.cpp
@@ -0,0 +1,18 @@
1#include <iostream>
2
3template<auto X> void println() { std::cout << X << std::endl; }
4
5int main() {
6 println<1.23>();
7 println<42>();
8
9 // The following attempts to print a string do not work
10 // uncomment to see why
11 // 1.
12 // println<"Hello!">();
13 // 2.
14 // constexpr std::string_view hello = "Hello!";
15 // println<hello>();
16
17 return 0;
18}
diff --git a/templates/std_array.cpp b/templates/std_array.cpp
new file mode 100644
index 0000000..8044cb2
--- /dev/null
+++ b/templates/std_array.cpp
@@ -0,0 +1,16 @@
1#include <array>
2#include <iostream>
3
4int main() {
5 std::array<double, 5> a {1.0, 1.1, 1.2};
6 std::cout << "Capacity of a: " << a.size() << std::endl;
7
8 a[3] = 1.3;
9 a[4] = 1.4;
10
11 for (auto x : a)
12 std::cout << x << " ";
13 std::cout << std::endl;
14
15 return 0;
16}

Generated with cgit - Back to sebastiano.tronto.net