<tuple>

tuple

public member function
<tuple>

std::tuple::tuple

default (1)
constexpr tuple();
copy / move (2)
tuple (const tuple& tpl) = default;tuple (tuple&& tpl) = default;
implicit conversion (3)
template <class... UTypes>  tuple (const tuple<UTypes...>& tpl);template <class... UTypes>  tuple (tuple<UTypes...>&& tpl);
initialization (4)
explicit tuple (const Types&... elems);template <class... UTypes>  explicit tuple (UTypes&&... elems);
conversion from pair (5)
template <class U1, class U2>  tuple (const pair<U1,U2>& pr);template <class U1, class U2>  tuple (pair<U1,U2>&& pr);
allocator (6)
template<class Alloc>  tuple (allocator_arg_t aa, const Alloc& alloc);template<class Alloc>  tuple (allocator_arg_t aa, const Alloc& alloc, const tuple& tpl);template<class Alloc>  tuple (allocator_arg_t aa, const Alloc& alloc, tuple&& tpl);template<class Alloc,class... UTypes>  tuple (allocator_arg_t aa, const Alloc& alloc, const tuple<UTypes...>& tpl);template<class Alloc, class... UTypes>  tuple (allocator_arg_t aa, const Alloc& alloc, tuple<UTypes...>&& tpl);template<class Alloc>  tuple (allocator_arg_t aa, const Alloc& alloc, const Types&... elems);template<class Alloc, class... UTypes>  tuple (allocator_arg_t aa, const Alloc& alloc, UTypes&&... elems);template<class Alloc, class U1, class U2>  tuple (allocator_arg_t aa, const Alloc& alloc, const pair<U1,U2>& pr);template<class Alloc, class U1, class U2>  tuple (allocator_arg_t aa, const Alloc& alloc, pair<U1,U2>&& pr);
Construct tuple
Constructs a tuple object.
This involves individually constructing its elements, with an initialization that depends on the constructor form invoked:

(1) default constructor
Constructs a tuple object with its elements value-initialized.
(2) copy / move constructor
The object is initialized with the contents of the tpl tuple object.
The corresponding element of tpl is passed to the constructor of each element.
(3) implicit conversion constructor
Same as above.
All types in tpl shall be implicitly convertible to the type of their respective element in the constructed tuple object.
(4) initialization constructor
Initializes each element with the corresponding element in elems.
The corresponding element of elems is passed to the constructor of each element.
(5) pair conversion constructor
The object has two elements corresponding to pr.first and pr.second. All types in pr shall be implicitly convertible to the type of their respective element in the tuple object.
(6) allocator versions
Same as the versions above, except that each element is constructed using allocator alloc.

Most forms have two signatures: one taking const lvalue references, which copies the elements into the tuple, and one taking rvalue references, which moves them instead if their types support move semantics (i.e., the contents are transferred to the tuple object and lost by their previous referrers, which are left in an unspecified but valid state).

Parameters

tpl
Another tuple object with the same number of elements.
This may be an object of the same type as the object being constructed (version 2) or of a tuple type whose elements' types are implicitly convertible to those in the tuple object being constructed.
elems...
A series of elements, each of their corresponding type in Types or UTypes template parameters.
pr
A pair object whose first and second types (U1 and U2) match those used as class template arguments, or are implicitly convertible to them.
aa
The std::allocator_arg value. This constant value is merely used to select the constructor forms with an allocator parameter.
alloc
Allocator object used to construct the elements.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// tuple constructors
#include <iostream>     // std::cout
#include <utility>      // std::make_pair
#include <tuple>        // std::tuple, std::make_tuple, std::get

int main ()
{
  std::tuple<int,char> first;                             // default
  std::tuple<int,char> second (first);                    // copy
  std::tuple<int,char> third (std::make_tuple(20,'b'));   // move
  std::tuple<long,char> fourth (third);                   // implicit conversion
  std::tuple<int,char> fifth (10,'a');                    // initialization
  std::tuple<int,char> sixth (std::make_pair(30,'c'));    // from pair / move

  std::cout << "sixth contains: " << std::get<0>(sixth);
  std::cout << " and " << std::get<1>(sixth) << '\n';

  return 0;
}

Output:
sixth contains: 30 and c


Data races

The elements of tpl and pr are accessed.
The constructors taking rvalue references as arguments modify these arguments if their types support move semantics for this construction.

Exception safety

If none of the individual constructions of elements of tuple can throw, the operation never throws exceptions (no-throw guarantee).
Otherwise, if any of the signatures taking an rvalue reference as argument is called, and at least one of the types in the tuple can be constructed with move semantics, the operation may leave tpl (or pr) in an invalid state in case of exception (no guarantees).
Otherwise, the function only implies copies and the operation produces no side effects (strong guarantee).

See also