public member function

std::tuple::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, initializing its contents depending on the constructor version used:

(1) default constructor
Constructs a tuple object where all its elements have been default-constructed.
(2) copy / move constructor
The object is initialized to have the same contents and properties as the tpl tuple object.
(3) implicit conversion constructor
Initializes each element with its corresponding counterpart in tpl. All types in tpl must be implicitly convertible to the type of their respective element in the tuple object.
(4) initialization constructor
Initializes each element with the corresponding paramater in elems.
(5) pair conversion constructor
The object has two elements corresponding to pr.first and pr.second. All types in pr must 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 versions have two signatures: one taking const lvalue references, which copies the elements into the tuple, and one taking a rvalue reference, which moves them instead (i.e., the contents are acquired by the tuple object without performing any copies and lost by their previous referrers).

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 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 signal that the signature to be used has an allocator parameter.
alloc
Allocator object to be used instead of constructing a new one.
For class instantiations using their version of the default allocator class template, this parameter is not relevant.

Example

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

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) << std::endl;

  return 0;
}


Output:
sixth contains: 30 and c

See also