Tuples

#include "tuple.tcc"

Tuples

Tuples can be used to group objects of different types. A Tuple is recursively defined as being either:

  • Empty.

  • A pair (head, tail), where head is of an arbitrary type and tail is an other Tuple.

Initialisation of a Tuple can be done with a brace-initializer-list as follows.

Tuple<int, char> t {10, 'c'};

Element retrieval and assignment is described below in the Helper functions section.

Note that a Tuple, like any higher order data structure, should be passed by reference.

Class definitions

template<class...>
class Tuple

Empty Tuple.

template<class T, class ...Ts>
class Tuple<T, Ts...>

Tuple.

Helper functions

Elements of a Tuple can be retrieved in two ways, either via the head and tail member variables, or using with the get<>() helper function.

int i = t.head;
char c = t.tail.head;

int j = get<0>(t);
char d = get<1>(t)';

Likewise, assignment of an element can be done via its member variables or with the get<>() helper function.

t.head = 11;
t.tail.head = 'd';

get<0>(t) = 11;
get<1>(t) = 'd';

There are additional helper functions available for the creation of Tuples.

The function makeTuple() can be used to create a temporary Tuple to be used in a function call.

function(makeTuple('a', 'b', 10));

Functions

template<size_t k, class ...Ts>
get(Tuple<Ts...> &t)

Get the k-th element of a Tuple.

This can be used for both retrieval as well as assignment.

Parameters

t – A Tuple.

Returns

Reference to the k-th element in t.

template<class ...Ts>
Tuple<Ts...> makeTuple(Ts... args)

Make a Tuple from a parameter pack.

Parameters

args – Values to store in a Tuple.

Returns

Tuple containing args.