From 2da2c56c72e7085bd9a52f848df90c6fc701de7f Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 19 Sep 2026 22:04:44 +0200 Subject: docs: add tutorial --- tutorial/04_conditions.sfinae | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tutorial/04_conditions.sfinae (limited to 'tutorial/04_conditions.sfinae') 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::value, +// ::less::value, or ::is_same::value. the ::value is mandatory +// a condition without it is a type, and a type is not a condition. + +// ::conditional_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> 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 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 is the same idea for values. this prints 9 + +::emit< + ::decltype( + ::conditional< + ::false_type::value, + ::difference<'A', 'A'>, + ::difference<'J', 'A'> + > + ) +>::commit; -- cgit v1.3