File: length.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (50 lines) | stat: -rw-r--r-- 2,229 bytes parent folder | download | duplicates (4)
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
As the number of types in a parameter pack may be obtained using the tt(sizeof)
operator (cf. section ref(VARIADIC)) it is easy to obtain the number of types
that were specified with a certain tt(TypeList). For example, the following
statement displays the value 3:
        verb(    std::cout << TypeList<int, char, bool>::size << '\n';)

However, it's illustrative to see how the number of types specified with a
tt(TypeList) could be determined if tt(sizeof) hadn't been available.

To obtain the number of types that were specified with a tt(TypeList)
the following algorithm is used:
    itemization(
    it() If the tt(TypeList) contains no types, its size equals zero;
    it() If the tt(TypeList) contains types, its size equals 1 plus the number
        of types that follow its  first type.
    )
    The algorithm uses recursion to define the length of a tt(TypeList). In
executable bf(C++) recursion could also be used in comparable situations. For
example recursion can be used to determine the length of an NTBS:
        verb(    size_t c_length(char const *cp)
    {
        return *cp == 0 ? 0 : 1 + c_length(cp + 1);
    })

While bf(C++) functions usually use iteration rather than recursion,
iteration is not available to template meta programming algorithms. In
template meta programming repetition em(must) be implemented using
recursion. Furthermore, while bf(C++) run-time code may use conditions to
decide whether or not to start the next recursion template meta programming
cannot do so. Template meta programming algorithms must resort to (partial)
specializations. The specializations are used to select alternatives.

    The number of types that are specified in a tt(TypeList) can be computed
using the following alternate implementation of tt(TypeList), using a generic
tt(struct) declaration and two specialization for the empty and non-empty
tt(TypeList) (cf. the above description of the algorithm):
        verb(    template <typename ...Types>
    struct TypeList;

    template <typename Head, typename ...Tail>
    struct TypeList<Head, Tail...>
    {
        enum { size = 1 + TypeList<Tail...>::size };
    };
    template <>
    struct TypeList<>
    {
        enum { size = 0 };
    };)