• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#metafunction]
2[section Template metafunction]
3
4A ['template metafunction] represents a function over types that is evaluated at
5compile-time. It is implemented by a template class.
6
7The template arguments of that class are expected to be types (`class` or
8`typename` template arguments). They represent the arguments of the
9metafunction.
10
11Instances of the template class are expected to have a public nested type called
12`type`. This type is the type the metafunction returns.
13
14Template metafunction are expected to be called with
15[link metaprogramming_value template metaprogramming values] as arguments only.
16
17Template metafunctions are expected to return template metaprogramming values.
18
19For example this is the identity template metafunction:
20
21  template <class T>
22  struct identity
23  {
24    using type = T;
25  };
26
27This metafunction is called `identity`. It takes one argument, `T`. The result
28of calling this metafunction is its argument, `T`.
29
30To call this metafunction, one has to instantiate the template class. The
31template arguments it is instantiated with are the arguments the metafunction is
32called with. For example:
33
34  identity<std::integral_constant<int, 13>>::type
35
36The above example calls the metafunction `identity` with
37`std::integral_constant<int, 13>` as its argument.
38
39[endsect]
40
41