Concatenating tuples

 

Doc. no.   N2299=07-0159
Date:        2007-06-05
Reply to:   Joe Gottman jgottman@carolina.rr.com

Library Working Group

 

Introduction

Proposed Wording

 

Introduction

 

    Conceptually, concatenation is a basic operation on tuples.  To concatenate two tuples just means to create a new tuple containing all the information in both tuples.  For example, if t is a tuple<int, string> containing {1, "two"} and u is a tuple <double> containing {3.0}, then concatenate(t, u) would be a tuple<int, string, double> containing {1, “two", 3.0}.  This operation has many potential uses.  For example, a user might call a function that returns a tuple and want to add extra information. Also, concatenation can be a basic building block for more complex tuple-based metaprogramming.  

 

    As tuples are currently defined it is simple to write a function to concatenate tuples of known size, but it would be difficult to implement this function for tuples of indeterminate size, even using template metaprogramming and implementation functions templated on int. The user would have to use techniques similar to the implementation of std::bind described in N2080.   I therefore propose adding four new functions to the standard to concatenate two tuples: 

 

   template <class… TTypes, class… UTypes>

     tuple<TTypes…, UTypes…> concatenate(const tuple<TTypes…> &t, const tuple<UTypes…> &u);

 

and three similar functions passing one or both parameters by rvalue reference instead of const lvalue reference.

Proposed Wording

 

   Modify <tuple> by extending the synopsis of 20.3.1.2 to include the following:

 

      template <class… TTypes, class… UTypes>

     tuple<TTypes…, UTypes…> concatenate(const tuple<TTypes…> &t, const tuple<UTypes…> &u);

 

   template <class… TTypes, class… UTypes>

     tuple<TTypes…, UTypes…> concatenate(tuple<TTypes…> &&t, const tuple<UTypes…> &u);

 

   template <class… TTypes, class… UTypes>

     tuple<TTypes…, UTypes…> concatenate(const tuple<TTypes…> &t, tuple<UTypes…> &&u);

 

   template <class… TTypes, class… UTypes>

     tuple<TTypes…, UTypes…> concatenate(tuple<TTypes…> &&t, tuple<UTypes…> &&u);

 

 

Add the following functions to 20.3.1.2:

 

 

template <class… TTypes, class… UTypes>

  tuple<TTypes…, UTypes…> concatenate(const tuple<TTypes…> &t, const tuple<UTypes…> &u);

 

Requires: All the types in TTypes and UTypes are CopyConstructable.

Returns: A tuple constructed by copy-constructing its first sizeof…(TTypes) elements from the corresponding elements of t and copy-constructing its last sizeof…(UTypes) elements from the corresponding elements of u.

 

template <class… TTypes, class… UTypes>

  tuple<TTypes…, UTypes…> concatenate(tuple<TTypes…> &&t, const tuple<UTypes…> &u);

 

Requires: All the types in TTypes are MoveConstructible and all the types in UTypes are CopyConstructible.

Returns:  A tuple constructed by move-constructing its first sizeof…(TTypes) elements from the corresponding elements of t and copy-constructing its last sizeof…(UTypes) elements from the corresponding elements of u.

 

template <class… TTypes, class… UTypes>

  tuple<TTypes…, UTypes…> concatenate(const tuple<TTypes…> &t, tuple<UTypes…> &&u);

 

Requires: All the types in TTypes are CopyConstructible and all the types in UTypes are MoveConstructible.

Returns:   A tuple constructed by copy-constructing its first sizeof…(TTypes) elements from the corresponding elements of t and move-constructing its last sizeof…(UTypes) elements from the corresponding elements of u.

 

template <class… TTypes, class… UTypes>

  tuple<TTypes…, UTypes…> concatenate(tuple<TTypes…> &&t, tuple<UTypes…> &&u);

 

Requires:  All the types in TTypes and UTypes are MoveConstructable.

Returns:  A tuple constructed by move-constructing its first sizeof…(TTypes) elements from the corresponding elements of t and move-constructing its last sizeof…(UTypes) elements from the corresponding elements of u.