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/02_numbers.sfinae | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tutorial/02_numbers.sfinae (limited to 'tutorial/02_numbers.sfinae') diff --git a/tutorial/02_numbers.sfinae b/tutorial/02_numbers.sfinae new file mode 100644 index 0000000..76f7dca --- /dev/null +++ b/tutorial/02_numbers.sfinae @@ -0,0 +1,61 @@ +// lesson 2: numbers +// +// a digit is a syntax error. the only number literal is ::difference<'a', 'b'>, +// the distance between two characters. 'F' - 'A' is 5. ::emit takes a type, and a +// value is not a type, so wrap it in ::decltype(...) to make it one + +::emit<::decltype(::difference<'F', 'A'>)>::commit; + +// arithmetic is ::sum, ::product and ::quotient. +// no operators +// no precedence +// no problem + +::emit< + ::decltype( + ::sum< + ::difference<'F', 'A'>, + ::difference<'C', 'A'> + > + ) +>::commit; + +::emit< + ::decltype( + ::product< + ::difference<'D', 'A'>, + ::difference<'E', 'A'> + > + ) +>::commit; + +::emit< + ::decltype( + ::quotient< + ::difference<'H', 'A'>, + ::difference<'C', 'A'> + > + ) +>::commit; + +// ::integral_constant<::size_t, V>::value is V. it exists so you can write more +// ::extent::value is the length of an array type, and 0 for anything else +// this is the long way to write 20 + +::emit< + ::decltype( + ::extent< + ::array< + ::byte, + ::integral_constant< + ::size_t, + ::difference<'Z', 'F'> + >::value + > + >::value + ) +>::commit; + +// emitting a type that is not a value prints the name of the type + +::emit<::remove_cv_t<::int32_t>>::commit; -- cgit v1.3