aboutsummaryrefslogtreecommitdiffhomepage
path: root/development/c++
diff options
context:
space:
mode:
Diffstat (limited to 'development/c++')
-rw-r--r--development/c++/meta.cc4
-rw-r--r--development/c++/meta3.cc22
2 files changed, 26 insertions, 0 deletions
diff --git a/development/c++/meta.cc b/development/c++/meta.cc
index e674cfd..efb3b22 100644
--- a/development/c++/meta.cc
+++ b/development/c++/meta.cc
@@ -2,6 +2,8 @@
#include <iostream>
+// -- Example 1 - print template value arguments.
+
// Base case with one parameter.
template<int P>
void show_int() {
@@ -15,6 +17,8 @@ void show_int() {
show_int<P1, Params...>();
}
+// -- Example 2 - print values of different types.
+
// Base case with one parameter.
template<typename T>
void show(const T& t) {
diff --git a/development/c++/meta3.cc b/development/c++/meta3.cc
new file mode 100644
index 0000000..5dce54a
--- /dev/null
+++ b/development/c++/meta3.cc
@@ -0,0 +1,22 @@
+// Copyright (C) 2022 johannst
+
+#include <type_traits>
+
+template<typename T, typename... U>
+struct any_of : std::false_type {};
+
+// Found our type T in the list of types U.
+template<typename T, typename... U>
+struct any_of<T, T, U...> : std::true_type {};
+
+// Pop off the first element in the list of types U,
+// since it didn't match our type T.
+template<typename T, typename U0, typename... U>
+struct any_of<T, U0, U...> : any_of<T, U...> {};
+
+// Convenience template variable to invoke meta function.
+template<typename T, typename... U>
+constexpr bool any_of_v = any_of<T, U...>::value;
+
+static_assert(any_of_v<int, char, bool, int>, "");
+static_assert(!any_of_v<int, char, bool, float>, "");