File: Metafunctions.rst

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (90 lines) | stat: -rw-r--r-- 2,896 bytes parent folder | download | duplicates (9)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
.. sidebar:: ToC

    .. contents::

.. _tutorial-how-to-recipes-metafunctions:

Metafunctions
=============

Type Metafunctions
------------------

For example, the metafunction :dox:`ContainerConcept#Iterator` is a type metafunction, i.e. it is used to determine a type.
Type metafunctions have the form:

``typename TypeMetaFunc<T1, T2, ..., TN>::Type``

``TypeMetaFunc``
  The name of the metafunction

``T1, T2, ..., TN``
  Arguments (types or constants)

``Type``
  The resulting type

The keyword ``typename`` must be stated if one of the arguments ``T1, T2, ..., TN`` is or uses a template parameter.
For example the following piece of code uses the metafunction ``Iterator`` to determine an iterator type for a string class:

.. includefrags:: demos/tutorial/metafunctions/base.cpp
    :fragment: iterator

.. includefrags:: demos/tutorial/metafunctions/base.cpp.stdout
    :fragment: iterator

Value Metafunctions
-------------------

Metafunctions can also be used to determine constant values at compile time.
The general form of value metafunctions is:

``VALUE_META_FUNC<T1, T2, ..., TN>::VALUE``

``VALUE_META_FUNC``
  The name of the metafunction

``T1, T2, ..., TN``
  Arguments (types or constants)

``VALUE``
  The resulting constant value

For example the following function prints the length of a fixed sized string using the value metafunction :dox:`LENGTH`:

.. includefrags:: demos/tutorial/metafunctions/base.cpp
    :fragment: length

.. includefrags:: demos/tutorial/metafunctions/base.cpp.stdout
    :fragment: length

.. important::
      Different uses of "*Value*":

      Please note that :dox:`ContainerConcept#Value` (``Value<TSomeType>::Type``) is a **Type Metafunction**, because it returns a Type (e.g. of values in a container) and not a value.

Assignment 1
""""""""""""

.. container:: assignment

   Objective
     Write a generic function ``checkContainerForDna(T & container)`` that prints out a message if the value inside the container is of the type :dox:`Dna`. The type ``T`` of the container should be specified as a template argument. Test you function with some examples.

   Hint
      * Use the **Type Metafunction** :dox:`ContainerConcept#Value` to access the (alphabet-)type of the elements in the container.
      * Use the **Value Metafunction** :dox:`IsSameType` to check for type equality.

   Solution
     .. container:: foldable

        Your program should look something like this:

        .. includefrags:: demos/tutorial/metafunctions/assignment1_solution.cpp

        Note: Because the Value Metafunction ``IsSameType<>`` is evaluated at compile time, the part of the if-statement code that does not apply won't even appear in the compiled code. This can be an improvement to the runtime of your code.

        The output is the following:

        .. includefrags:: demos/tutorial/metafunctions/assignment1_solution.cpp.stdout