# Tuple library advanced features

## Metafunctions for tuple types

Suppose `T` is a tuple type, and `N` is a constant integral expression. ``

    tuple_element<N, T>::type

gives the type of the `N`th element in the tuple type `T`. `

    tuple_length<T>::value

` gives the length of the tuple type `T`.

## Cons lists

Tuples are internally represented as cons lists. For example, the tuple ``

    tuple<A, B, C, D>

inherits from the type `

    cons<A, cons<B, cons<C, cons<D, null_type> > > >

` The tuple template provides the typedef `inherited` to access the cons list representation. E.g.: `tuple<A>::inherited` is the type `cons<A, null_type>`.

#### Empty tuple

The internal representation of the empty tuple `tuple<>` is `null_type`.

#### Head and tail

Both tuple template and the cons templates provide the typedefs `head_type` and `tail_type`. The `head_type` typedef gives the type of the first element of the tuple (or the cons list). The `tail_type` typedef gives the remaining cons list after removing the first element. The head element is stored in the member variable `head` and the tail list in the member variable `tail`. Cons lists provide the member function `get_head()` for getting a reference to the head of a cons list, and `get_tail()` for getting a reference to the tail. There are const and non-const versions of both functions.

Note that in a one element tuple, `tail_type` equals `null_type` and the `get_tail()` function returns an object of type `null_type`.

The empty tuple (`null_type`) has no head or tail, hence the `get_head` and `get_tail` functions are not provided.

Treating tuples as cons lists gives a convenient means to define generic functions to manipulate tuples. For example, the following pair of function templates assign 0 to each element of a tuple (obviously, the assignments must be valid operations for the element types):

    inline void set_to_zero(const null_type&) {};

    template <class H, class T>
    inline void set_to_zero(cons<H, T>& x) { x.get_head() = 0; set_to_zero(x.get_tail()); }

## Traits classes for tuple element types

#### `tuple_access_traits`

The template `tuple_access_traits` defines three type functions. Let `T` be a type of an element in a tuple:

1. `tuple_access_traits<T>::type` maps `T` to the return type of the non-const access functions (nonmeber and member `get` functions, and the `get_head` function).
2. `tuple_access_traits<T>::const_type` maps `T` to the return type of the const access functions.
3. `tuple_access_traits<T>::parameter_type` maps `T` to the parameter type of the tuple constructor.

#### `make_tuple_traits`

The element types of the tuples that are created with the `make_tuple` functions are computed with the type function `make_tuple_traits`. The type function call `make_tuple_traits<T>::type` implements the following type mapping:

- any reference type -> compile time error
- any array type -> constant reference to the array type
- `reference_wrapper<T>` -> `T&`
- `T` -> `T`

Objects of type `reference_wrapper` are created with the `ref` and `cref` functions (see The `make_tuple` function.)

Note, that the `reference_wrapper` template and the `ref` and `cref` functions are defined in a separate hpp-file `reference_wrappers.hpp`, which can be included without including the rest of the tuple library.

Back to the user's guide

© Copyright Jaakko Järvi 2001.