(Danny Kalev Posted on 2004-12-27 16:11:06)
The C Standards Committee is performing the extension of Standard Library. This extension includes a new Tuple library. Below I will discuss this library and its application. (There is currently not included in the standard library file, so you need to download it from Boost: http://sourceforge.net/project/showfiles.php? Group_id = 7586
1, what is TUPLE?
TUPLE is a set of fixed heterogeneous objects. TUPLE types There are many useful applications, such as packaging a function of multiple return values and simultaneous assignments and comparisons for multiple objects.
The size of the tuple refers to the number of elements it contains. The current TUPLE library supports Tuple of 0-10 elements. Each element can be different types. The following example creates a TUPLE with two elements (Float and Void *) and a matching initialization routine.
#include
If the initialization routine is ignored, the default initialization process is used:
TUPLE
2, auxiliary function
The Tuple library includes several auxiliary functions, for example, the make_tuple () function instantiates a TUPLE type according to its parameters:
VoidFunc (INT N); Make_Tuple (FUNC); // Returns: tuple
The tuple_size () function returns a size of a tuple:
INT n = tuple_size
The TUPLE_ELEMENT () function retrieves the type of a single element. This function receives an index and tuple type:
// Get thefirst Element's type, i.e., floatt = tuple_element <0, tuple
To access the elements itself, use the get () function template. Template parameters (which is also the parameters enclosed in angle brackets) are indexes of elements, and the parameters in parentheses are TUPLE types:
TUPLE
3, application
Tuple can be used to encapsulate multiple return values of a function. E.g:
TypeDeftuple