aboutsummaryrefslogtreecommitdiffhomepage
path: root/development
diff options
context:
space:
mode:
authorjohannst <johannst@users.noreply.github.com>2022-08-24 16:10:10 +0000
committerjohannst <johannst@users.noreply.github.com>2022-08-24 16:10:10 +0000
commit865999619eb8e79a3e36ac17f782c7c302ca923e (patch)
tree797a40e2ebd99febb6369d4f235ffa16041a6a2c /development
parent25b609d0c70d49dd62479ce03578704e62712bd8 (diff)
downloadnotes-865999619eb8e79a3e36ac17f782c7c302ca923e.tar.gz
notes-865999619eb8e79a3e36ac17f782c7c302ca923e.zip
deploy: e6f439cb43ee51b13ed4d29ec84ac5450556b163
Diffstat (limited to 'development')
-rw-r--r--development/c++.html35
-rw-r--r--development/c++/meta.cc4
-rw-r--r--development/c++/meta3.cc22
3 files changed, 60 insertions, 1 deletions
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)::_;
<h2 id="variadic-templates-parameter-pack"><a class="header" href="#variadic-templates-parameter-pack">Variadic templates (<a href="https://en.cppreference.com/w/cpp/language/parameter_pack">parameter pack</a>)</a></h2>
<pre><code class="language-cpp">#include &lt;iostream&gt;
+// -- Example 1 - print template value arguments.
+
// Base case with one parameter.
template&lt;int P&gt;
void show_int() {
@@ -161,6 +163,8 @@ void show_int() {
show_int&lt;P1, Params...&gt;();
}
+// -- Example 2 - print values of different types.
+
// Base case with one parameter.
template&lt;typename T&gt;
void show(const T&amp; t) {
@@ -179,7 +183,36 @@ int main() {
show(1, 1.0, &quot;foo&quot;, 'a');
}
</code></pre>
-<h2 id="sfinae-example-enable_if"><a class="header" href="#sfinae-example-enable_if"><a href="https://en.cppreference.com/w/cpp/language/sfinae">SFINAE</a> example (<a href="https://en.cppreference.com/w/cpp/types/enable_if">enable_if</a>)</a></h2>
+<h2 id="example-is_any_of-template-meta-function"><a class="header" href="#example-is_any_of-template-meta-function">Example: <code>is_any_of</code> template meta function</a></h2>
+<pre><code class="language-cpp">#include &lt;type_traits&gt;
+
+template&lt;typename T, typename... U&gt;
+struct any_of : std::false_type {};
+
+// Found our type T in the list of types U.
+template&lt;typename T, typename... U&gt;
+struct any_of&lt;T, T, U...&gt; : std::true_type {};
+
+// Pop off the first element in the list of types U,
+// since it didn't match our type T.
+template&lt;typename T, typename U0, typename... U&gt;
+struct any_of&lt;T, U0, U...&gt; : any_of&lt;T, U...&gt; {};
+
+// Convenience template variable to invoke meta function.
+template&lt;typename T, typename... U&gt;
+constexpr bool any_of_v = any_of&lt;T, U...&gt;::value;
+
+static_assert(any_of_v&lt;int, char, bool, int&gt;, &quot;&quot;);
+static_assert(!any_of_v&lt;int, char, bool, float&gt;, &quot;&quot;);
+</code></pre>
+<h2 id="example-sfinae-enable_if"><a class="header" href="#example-sfinae-enable_if">Example: <a href="https://en.cppreference.com/w/cpp/language/sfinae">SFINAE</a> (<a href="https://en.cppreference.com/w/cpp/types/enable_if">enable_if</a>)</a></h2>
+<p>Provide a single entry point <code>Invoke</code> to call some <code>Operations</code>.
+Use <code>enable_if</code> to enable/disable the template functions depending on the two
+available traits an operation can have:</p>
+<ul>
+<li>Operation returns a result</li>
+<li>Operation requires a context</li>
+</ul>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;type_traits&gt;
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>, "");