Tuples |
Tuples are the most basic infrastructure that the framework builds with. This sub-library provides a mechanism to bundle objects of arbitrary types in a single structure. Tuples hold heterogeneous types up to a predefined maximum.
Only the most basic functionality needed are provided. This is a straight-forward and extremely lean and mean library. Unlike other recursive list-like tuple implementations, this tuple library implementation uses simple structs similar to std::pair with specialization for 0 to N tuple elements, where N is a predefined constant. There are only 4 tuple operations to learn:
1) Construction
Here are examples on how to construct tuples:
typedef tuple<int, char> t1_t;
typedef tuple<int, std::string, double> t2_t;
// this tuple has an int and char members
t1_t t1(3, 'c');
// this tuple has an int, std::string and double members
t2_t t2(3, "hello", 3.14);
2) Member access
A member in a tuple can be accessed using the tuple's operator by specifying the Nth tuple_index. Here are some examples:
tuple_index<0> ix0; // 0th index == 1st item
tuple_index<1> ix1; // 1st index == 2nd item
tuple_index<2> ix2; // 2nd index == 3rd item
// Note zero based indexing. 0 = 1st item, 1 = 2nd item
t1[ix0] = 33; // sets the int member of the tuple t1
t2[ix2] = 6e6; // sets the double member of the tuple t2
t1[ix1] = 'a'; // sets the char member of the tuple t1
Access to out of bound indexes returns a nil_t value.
3) Member type inquiry
The type of an individual member can be queried. Example:
tuple_element<1, t2_t>::type
Refers to the type of the second member (again note zero based indexing, hence 0 = 1st item, 1 = 2nd item) of the tuple.
Access to out of bound indexes returns a nil_t type.
4) Tuple length
The number of elements in a tuple can be queried. Example:
int n = t1.length;
gets the number of elements in tuple t1.
length is a static constant. Thus, TupleT::length also works. Example:
int n = t1_t::length;
Copyright © 2001-2002 Joel de Guzman
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.