std::pair<T1,T2>::pair (3) - Linux Manuals

std::pair<T1,T2>::pair: std::pair<T1,T2>::pair

NAME

std::pair<T1,T2>::pair - std::pair<T1,T2>::pair

Synopsis


pair(); (until C++11)
constexpr pair(); (since C++11)
                                                    (conditionally explicit)
pair( const T1& x, const T2& y ); (until C++11)
                                                                             (since C++11)
pair( const T1& x, const T2& y ); (until C++14)
                                                                             (conditionally explicit)
constexpr pair( const T1& x, const T2& y ); (since C++14)
                                                                             (conditionally explicit)
template< class U1, class U2 > (since C++11)
pair( U1&& x, U2&& y ); (until C++14)
                                                                                                      (conditionally explicit)
template< class U1, class U2 > (since C++14)
constexpr pair( U1&& x, U2&& y ); (conditionally explicit)
template< class U1, class U2 > (until C++11)
pair( const pair<U1, U2>& p );
template< class U1, class U2 > (since C++11)
pair( const pair<U1, U2>& p ); (1) (until C++14)
                                                                                                                               (conditionally explicit)
template< class U1, class U2 > (2) (since C++14)
constexpr pair( const pair<U1, U2>& p ); (conditionally explicit)
template< class U1, class U2 > (since C++11)
pair( pair<U1, U2>&& p ); (3) (until C++14)
                                                                                                                                                        (conditionally explicit)
template< class U1, class U2 > (4) (since C++14)
constexpr pair( pair<U1, U2>&& p ); (conditionally explicit)
template< class... Args1, class... Args2 >
pair( std::piecewise_construct_t, (since C++11)
std::tuple<Args1...> first_args, (5) (until C++20)
std::tuple<Args2...> second_args );
template< class... Args1, class... Args2 > (6)
constexpr pair( std::piecewise_construct_t, (since C++20)
std::tuple<Args1...> first_args,
std::tuple<Args2...> second_args );
pair( const pair& p ) = default; (7)
pair( pair&& p ) = default; (8) (since C++11)


Constructs a new pair.
1) Default constructor. Value-initializes both elements of the pair, first and second.


* This constructor participates in overload resolution if and only if std::is_default_constructible_v<first_type> and std::is_default_constructible_v<second_type> are both true. (since C++11)
* This constructor is explicit if and only if either first_type or second_type is not implicitly default-constructible.


2) Initializes first with x and second with y.


* This constructor participates in overload resolution if and only if std::is_copy_constructible_v<first_type> and std::is_copy_constructible_v<second_type> are both true. (since C++11)
* This constructor is explicit if and only if std::is_convertible_v<const first_type&, first_type> is false or std::is_convertible_v<const second_type&, second_type> is false.


3) Initializes first with std::forward<U1>(x) and second with std::forward<U2>(y).


* This constructor participates in overload resolution if and only if std::is_constructible_v<first_type, U1&&> and std::is_constructible_v<second_type, U2&&> are both true.
* This constructor is explicit if and only if std::is_convertible_v<U1&&, first_type> is false or std::is_convertible_v<U2&&, second_type> is false.


4) Initializes first with p.first and second with p.second.


* This constructor participates in overload resolution if and only if std::is_constructible_v<first_type, const U1&> and std::is_constructible_v<second_type, const U2&> are both true. (since C++11)
* This constructor is explicit if and only if std::is_convertible_v<const U1&, first_type> is false or std::is_convertible_v<const U2&, second_type> is false.


5) Initializes first with std::forward<U1>(p.first) and second with std::forward<U2>(p.second).


* This constructor participates in overload resolution if and only if std::is_constructible_v<first_type, U1&&> and std::is_constructible_v<second_type, U2&&> are both true.
* This constructor is explicit if and only if std::is_convertible_v<U1&&, first_type> is false or std::is_convertible_v<U2&&, second_type> is false.


6) Forwards the elements of first_args to the constructor of first and forwards the elements of second_args to the constructor of second. This is the only non-default constructor that can be used to create a pair of non-copyable non-movable types.
7) Copy constructor is defaulted, and is constexpr if copying of both elements satisfies the requirements on constexpr functions.
8) Move constructor is defaulted, and is constexpr if moving of both elements satisfies the requirements on constexpr functions.

Parameters


x - value to initialize the first element of this pair
y - value to initialize the second element of this pair
p - pair of values used to initialize both elements of this pair
first_args - tuple of constructor arguments to initialize the first element of this pair
second_args - tuple of constructor arguments to initialize the second element of this pair

Exceptions


Does not throw exceptions unless one of the specified operations (e.g. constructor of an element) throws.


Defect reports


The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


DR Applied to Behavior as published Correct behavior
N4387 C++11 some constructors were implicit-only, preventing some uses constructors made conditionally-explicit
LWG_2510 C++11 default constructor was implicit made conditionally-explicit

Example


// Run this code


  #include <utility>
  #include <string>
  #include <complex>
  #include <tuple>
  #include <iostream>


  int main()
  {
      std::pair<int, float> p1;
      std::cout << "Value-initialized: "
                << p1.first << ", " << p1.second << '\n';


      std::pair<int, double> p2(42, 0.123);
      std::cout << "Initialized with two values: "
                << p2.first << ", " << p2.second << '\n';


      std::pair<char, int> p4(p2);
      std::cout << "Implicitly converted: "
                << p4.first << ", " << p4.second << '\n';


      std::pair<std::complex<double>, std::string> p6(
                      std::piecewise_construct,
                      std::forward_as_tuple(0.123, 7.7),
                      std::forward_as_tuple(10, 'a'));
      std::cout << "Piecewise constructed: "
                << p6.first << ", " << p6.second << '\n';
  }

Output:


  Value-initialized: 0, 0
  Initialized with two values: 42, 0.123
  Implicitly converted: *, 0
  Piecewise constructed: (0.123,7.7), aaaaaaaaaa

See also


          creates a pair object of type, defined by the argument types
make_pair (function template)