hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/tutorial/04_conditions.sfinae
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-19 22:04:44 +0200
committerhachem <im@hachem.wtf>2026-09-19 22:04:44 +0200
commit2da2c56c72e7085bd9a52f848df90c6fc701de7f (patch)
tree96cf837a606297aed921b9adaac783940a1aa7cd /tutorial/04_conditions.sfinae
parent6487b5b5301f6cb37c95c3571578d5a42adb2a1a (diff)
docs: add tutorial
Diffstat (limited to 'tutorial/04_conditions.sfinae')
-rw-r--r--tutorial/04_conditions.sfinae74
1 files changed, 74 insertions, 0 deletions
diff --git a/tutorial/04_conditions.sfinae b/tutorial/04_conditions.sfinae
new file mode 100644
index 0000000..94e0cbf
--- /dev/null
+++ b/tutorial/04_conditions.sfinae
@@ -0,0 +1,74 @@
+// lesson 4: conditions
+//
+// a condition is ::true_type::value, ::false_type::value, ::equal<V, V>::value,
+// ::less<V, V>::value, or ::is_same<T, T>::value. the ::value is mandatory
+// a condition without it is a type, and a type is not a condition.
+
+// ::conditional_t<C, T, T> picks a type. this prints 1
+
+::emit<
+ ::conditional_t<
+ ::true_type::value,
+ ::decltype(::difference<'B', 'A'>),
+ ::void
+ >
+>::commit;
+
+// this picks ::void, and emitting ::void prints nothing at all. not even a newline
+
+::emit<
+ ::conditional_t<
+ ::less<
+ ::difference<'B', 'A'>,
+ ::difference<'A', 'A'>
+ >::value,
+ ::decltype(::difference<'Z', 'A'>),
+ ::void
+ >
+>::commit;
+
+// ::enable_if_t<C, ::sequence<...>> runs the statements when C holds
+// the statements inside a sequence are separated by commas, and each one is
+// still committed on its own
+
+::enable_if_t<
+ ::is_same<
+ ::remove_cv_t<::int32_t>,
+ ::int32_t
+ >::value,
+ ::sequence<
+ ::emit<
+ ::tuple<
+ ::integral_constant<::char, ::difference<'s', '\0'>>,
+ ::integral_constant<::char, ::difference<'a', '\0'>>,
+ ::integral_constant<::char, ::difference<'m', '\0'>>,
+ ::integral_constant<::char, ::difference<'e', '\0'>>
+ >
+ >::commit
+ >
+>::commit;
+
+// ::static_assert<C> stops the program when C does not hold. it does this at
+// runtime, because that is when we know
+
+::static_assert<
+ ::equal<
+ ::difference<'C', 'A'>,
+ ::sum<
+ ::difference<'B', 'A'>,
+ ::difference<'B', 'A'>
+ >
+ >::value
+>::commit;
+
+// ::conditional<C, V, V> is the same idea for values. this prints 9
+
+::emit<
+ ::decltype(
+ ::conditional<
+ ::false_type::value,
+ ::difference<'A', 'A'>,
+ ::difference<'J', 'A'>
+ >
+ )
+>::commit;