From 865999619eb8e79a3e36ac17f782c7c302ca923e Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 24 Aug 2022 16:10:10 +0000 Subject: deploy: e6f439cb43ee51b13ed4d29ec84ac5450556b163 --- development/c++.html | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'development/c++.html') diff --git a/development/c++.html b/development/c++.html index 2ae657c..cf33af4 100644 --- a/development/c++.html +++ b/development/c++.html @@ -148,6 +148,8 @@ typename decltype(foo)::_;

Variadic templates (parameter pack)

#include <iostream>
 
+// -- Example 1 - print template value arguments.
+
 // Base case with one parameter.
 template<int P>
 void show_int() {
@@ -161,6 +163,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) {
@@ -179,7 +183,36 @@ int main() {
     show(1, 1.0, "foo", 'a');
 }
 
-

SFINAE example (enable_if)

+

Example: is_any_of template meta function

+
#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>, "");
+
+

Example: SFINAE (enable_if)

+

Provide a single entry point Invoke to call some Operations. +Use enable_if to enable/disable the template functions depending on the two +available traits an operation can have:

+
#include <iostream>
 #include <type_traits>
 
-- 
cgit v1.2.3