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