hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/tutorial/05_loops.sfinae
blob: d974f99722e426259f751e0bc8c7c91c32567d4a (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
// lesson 5: loops
//
// ::repeat<V, ::sequence<...>> runs the sequence V times. that is the whole
// loop. there is no index, no condition, no break.

::repeat<
    ::difference<'D', 'A'>,
    ::sequence<
        ::emit<
            ::tuple<
        ::integral_constant<::char, ::difference<'h', '\0'>>,
        ::integral_constant<::char, ::difference<'i', '\0'>>
            >
        >::commit
    >
>::commit;

// counting needs a variable, and changing a variable needs the undeclare dance,
// and the dance needs a second variable to hold the value while the first one
// is dead. this prints 1, 2, 3

::declare<
    ::name<'i'>,
    ::decltype(::difference<'B', 'A'>)
>::commit;

::repeat<
    ::difference<'D', 'A'>,
    ::sequence<
        ::emit<::decltype(::value_of<::name<'i'>>)>::commit,
        ::declare<
            ::name<'n', 'e', 'x', 't'>,
            ::decltype(
                ::sum<
                    ::value_of<::name<'i'>>,
                    ::difference<'B', 'A'>
                >
            )
        >::commit,
        ::undeclare<::name<'i'>>::commit,
        ::declare<
            ::name<'i'>,
            ::decltype(::value_of<::name<'n', 'e', 'x', 't'>>)
        >::commit,
        ::undeclare<::name<'n', 'e', 'x', 't'>>::commit
    >
>::commit;