hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/tutorial/02_numbers.sfinae
blob: 76f7dca20faf51f30c8026dd6b2bdd2a1ddb4368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<T>::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;