tuple_advanced_interface.qbk 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. [/
  2. / Copyright (c) 2001 Jaakko Järvi
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See
  5. / accompanying file LICENSE_1_0.txt or copy at
  6. / http://www.boost.org/LICENSE_1_0.txt)
  7. /]
  8. [article Tuple library advanced features
  9. [quickbook 1.6]
  10. [id tuple_advanced_interface]
  11. [copyright 2001 Jaakko J\u00E4rvi]
  12. [license Distributed under the
  13. [@http://boost.org/LICENSE_1_0.txt Boost Software License,
  14. Version 1.0].
  15. ]
  16. ]
  17. [template simplesect[title]
  18. [block '''<simplesect><title>'''[title]'''</title>''']]
  19. [template endsimplesect[]
  20. [block '''</simplesect>''']]
  21. The advanced features described in this document are all under namespace
  22. `::boost::tuples`
  23. [section Metafunctions for tuple types]
  24. Suppose `T` is a tuple type, and `N` is a constant integral expression.
  25. element<N, T>::type
  26. gives the type of the `N`-th element in the tuple type `T`. If `T` is `const`,
  27. the resulting type is `const` qualified as well. Note that the constness of `T`
  28. does not affect reference type elements.
  29. length<T>::value
  30. gives the length of the tuple type `T`.
  31. [endsect]
  32. [section Cons lists]
  33. Tuples are internally represented as /cons lists/. For example, the tuple
  34. tuple<A, B, C, D>
  35. inherits from the type
  36. cons<A, cons<B, cons<C, cons<D, null_type> > > >
  37. The tuple template provides the typedef inherited to access the cons list
  38. representation. E.g.: `tuple<A>::inherited` is the type `cons<A, null_type>`.
  39. [section Empty tuple]
  40. The internal representation of the empty tuple `tuple<>` is `null_type`.
  41. [endsect]
  42. [section Head and tail]
  43. Both tuple template and the cons templates provide the typedefs `head_type`
  44. and `tail_type`. The `head_type` typedef gives the type of the first element
  45. of the tuple (or the cons list). The `tail_type` typedef gives the remaining
  46. cons list after removing the first element. The head element is stored in the
  47. member variable `head` and the tail list in the member variable `tail`. Cons
  48. lists provide the member function `get_head()` for getting a reference to the
  49. head of a cons list, and `get_tail()` for getting a reference to the tail.
  50. There are const and non-const versions of both functions.
  51. Note that in a one element tuple, `tail_type` equals `null_type` and the
  52. `get_tail()` function returns an object of type `null_type`.
  53. The empty tuple (`null_type`) has no head or tail, hence the `get_head` and
  54. `get_tail` functions are not provided.
  55. Treating tuples as cons lists gives a convenient means to define generic
  56. functions to manipulate tuples. For example, the following pair of function
  57. templates assign `0` to each element of a tuple (obviously, the assignments
  58. must be valid operations for the element types):
  59. inline void set_to_zero(const null_type&) {};
  60. template <class H, class T>
  61. inline void set_to_zero(cons<H, T>& x) { x.get_head() = 0; set_to_zero(x.get_tail()); }
  62. [endsect]
  63. [section Constructing cons lists]
  64. A cons list can be default constructed provided that all its elements can be
  65. default constructed.
  66. A cons list can be constructed from its head and tail. The prototype of the
  67. constructor is:
  68. cons(typename access_traits<head_type>::parameter_type h, const tail_type& t)
  69. The traits template for the head parameter selects correct parameter types for
  70. different kinds of element types (for reference elements the parameter type
  71. equals the element type, for non-reference types the parameter type is a
  72. reference to const non-volatile element type).
  73. For a one-element cons list the tail argument (`null_type`) can be omitted.
  74. [endsect]
  75. [endsect]
  76. [section Traits classes for tuple element types]
  77. [section access_traits]
  78. The template `access_traits` defines three type functions. Let `T` be a type
  79. of an element in a tuple:
  80. * `access_traits<T>::non_const_type` maps `T` to the return type of the no
  81. n-const access functions (nonmember and member `get` functions, and the
  82. `get_head` function).
  83. * `access_traits<T>::const_type` maps `T` to the return type of the const
  84. access functions.
  85. * `access_traits<T>::parameter_type` maps `T` to the parameter type of the
  86. tuple constructor.
  87. [endsect]
  88. [section make_tuple_traits]
  89. The element types of the tuples that are created with the `make_tuple`
  90. functions are computed with the type function `make_tuple_traits`. The type
  91. function call `make_tuple_traits<T>::type` implements the following type
  92. mapping:
  93. * /any reference type/ -> /compile time error/
  94. * /any array type/ -> /constant reference to the array type/
  95. * `reference_wrapper<T>` -> `T&`
  96. * `T` -> `T`
  97. Objects of type `reference_wrapper` are created with the `ref` and `cref`
  98. functions (see [link tuple.constructing_tuples.make_tuple The `make_tuple`
  99. function]).
  100. Reference wrappers were originally part of the tuple library, but they are now
  101. a general utility of boost. The `reference_wrapper` template and the `ref` and
  102. `cref` functions are defined in a separate file
  103. [@boost:/libs/core/doc/html/core/ref.html `ref.hpp`] in the main boost include
  104. directory; and directly in the `boost` namespace.
  105. [endsect]
  106. [endsect]