]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#metafunction_class] |
2 | [section Template metafunction class] | |
3 | ||
4 | A ['template metafunction class] is a type with a public nested | |
5 | [link metafunction template metafunction] called `apply`. Since it is a type, it can be | |
6 | passed to template metafunctions as arguments and metafunctions can return it as | |
7 | their result. This makes it possible to implement | |
8 | ['higher-order template metafunctions], which are template metafunctions taking | |
9 | template metafunctions as arguments or returning template metafunctions as their | |
10 | result. | |
11 | ||
12 | For example this is the identity template metafunction class: | |
13 | ||
14 | struct identity | |
15 | { | |
16 | template <class T> | |
17 | struct apply | |
18 | { | |
19 | using type = T; | |
20 | }; | |
21 | using type = identity; | |
22 | }; | |
23 | ||
24 | This metafunction class is called `identity`. It takes one argument, `T`. The | |
25 | result of calling this metafunction class is its argument, `T`. Note that the | |
26 | `identity` metafunction class is also a | |
27 | [link metaprogramming_value template metaprogramming value], so it can be an | |
28 | argument or the result of a template metafunction. | |
29 | ||
30 | To call this metafunction, one has to call the nested template metafunction. | |
31 | For example: | |
32 | ||
33 | identity::apply<std::integral_constant<int, 13>>::type | |
34 | ||
35 | The above example calls the metafunction class `identity` with | |
36 | `std::integral_constant<int, 13>` as its argument. | |
37 | ||
38 | [endsect] | |
39 |