Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |utility‎ |pair
       
       
      Utilities library
       
       
      pair();
      (1)(constexpr since C++11)
      (conditionally explicit since C++11)
      pair(const T1& x,const T2& y);
      (2)(conditionally explicit since C++11)
      (constexpr since C++14)
      (3)
      template<class U1,class U2>
      pair( U1&& x, U2&& y);
      (since C++11)
      (until C++23)
      (constexpr since C++14)
      (conditionally explicit)
      template<class U1= T1,class U2= T2>
      constexpr pair( U1&& x, U2&& y);
      (since C++23)
      (conditionally explicit)
      template<class U1,class U2>
      constexpr pair( pair<U1, U2>& p);
      (4)(since C++23)
      (conditionally explicit)
      template<class U1,class U2>
      pair(const pair<U1, U2>& p);
      (5)(conditionally explicit since C++11)
      (constexpr since C++14)
      template<class U1,class U2>
      pair( pair<U1, U2>&& p);
      (6)(constexpr since C++14)
      (conditionally explicit since C++11)
      template<class U1,class U2>
      constexpr pair(const pair<U1, U2>&& p);
      (7)(since C++23)
      (conditionally explicit)
      template< pair-like P>
      constexpr pair( P&& u);
      (8)(since C++23)
      (conditionally explicit)
      template<class...Args1,class...Args2>

      pair(std::piecewise_construct_t,
           std::tuple<Args1...> first_args,

           std::tuple<Args2...> second_args);
      (9)(since C++11)
      (constexpr since C++20)
      pair(const pair& p)=default;
      (10)
      pair( pair&& p)=default;
      (11)(since C++11)

      Constructs a new pair.

      1) Default constructor. Value-initializes both elements of the pair,first andsecond.

      This constructor participates in overload resolution if and only ifstd::is_default_constructible_v<T1> andstd::is_default_constructible_v<T2> are bothtrue.

      This constructor isexplicit if and only if eitherT1 orT2 is not implicitly default-constructible.

      (since C++11)
      2) Initializesfirst withx andsecond withy.

      This constructor participates in overload resolution if and only ifstd::is_copy_constructible_v<T1> andstd::is_copy_constructible_v<T2> are bothtrue.

      This constructor isexplicit if and only ifstd::is_convertible_v<const T1&, T1> isfalse orstd::is_convertible_v<const T2&, T2> isfalse.

      (since C++11)
      3) Initializesfirst withstd::forward<U1>(x) andsecond withstd::forward<U2>(y).
      This constructor participates in overload resolution if and only ifstd::is_constructible_v<T1, U1> andstd::is_constructible_v<T2, U2> are bothtrue.
      This constructor isexplicit if and only ifstd::is_convertible_v<U1, T1> isfalse orstd::is_convertible_v<U2, T2> isfalse.

      This constructor is defined as deleted if the initialization offirst orsecond wouldbind a reference to temporary object.

      (since C++23)
      4) Initializesfirst withp.first andsecond withp.second.
      This constructor participates in overload resolution if and only ifstd::is_constructible_v<T1, U1&> andstd::is_constructible_v<T2, U2&> are bothtrue.
      This constructor isexplicit if and only ifstd::is_convertible_v<U1&, T1> isfalse orstd::is_convertible_v<U2&, T2> isfalse.
      This constructor is defined as deleted if the initialization offirst orsecond would bind a reference to temporary object.
      5) Initializesfirst withp.first andsecond withp.second.

      This constructor participates in overload resolution if and only ifstd::is_constructible_v<T1,const U1&> andstd::is_constructible_v<T2,const U2&> are bothtrue.

      This constructor isexplicit if and only ifstd::is_convertible_v<const U1&, T1> isfalse orstd::is_convertible_v<const U2&, T2> isfalse.

      (since C++11)

      This constructor is defined as deleted if the initialization offirst orsecond would bind a reference to temporary object.

      (since C++23)
      6) Initializesfirst withstd::forward<U1>(p.first) andsecond withstd::forward<U2>(p.second).
      This constructor participates in overload resolution if and only ifstd::is_constructible_v<T1, U1> andstd::is_constructible_v<T2, U2> are bothtrue.
      This constructor isexplicit if and only ifstd::is_convertible_v<U1, T1> isfalse orstd::is_convertible_v<U2, T2> isfalse.

      This constructor is defined as deleted if the initialization offirst orsecond would bind a reference to temporary object.

      (since C++23)
      7) Initializesfirst withstd::forward<const U1>(p.first) andsecond withstd::forward<const U2>(p.second).
      This constructor participates in overload resolution if and only ifstd::is_constructible_v<T1, U1> andstd::is_constructible_v<T2, U2> are bothtrue.
      This constructor isexplicit if and only ifstd::is_convertible_v<const U1, T1> isfalse orstd::is_convertible_v<const U2, T2> isfalse.
      This constructor is defined as deleted if the initialization offirst orsecond would bind a reference to temporary object.
      8) Givenu1 asstd::get<0>(std::forward(u)) andu2 asstd::get<1>(std::forward(u)), denote their types asU1 andU2 respectively. Initializesfirst withu1 andsecond withu2.
      This constructor participates in overload resolution if and only if
      This constructor isexplicit if and only ifstd::is_convertible_v<U1, T1> isfalse orstd::is_convertible_v<U2, T2> isfalse.
      This constructor is defined as deleted if the initialization offirst orsecond would bind a reference to temporary object.
      9) Forwards the elements offirst_args to the constructor offirst and forwards the elements ofsecond_args to the constructor ofsecond. This is the only non-default constructor that can be used to create a pair of non-copyable non-movable types. The program is ill-formed iffirst orsecond is a reference and bound to a temporary object.
      10) Copy constructor isimplicitly declared(until C++11)defaulted, and isconstexpr if copying of both elements satisfies the requirements on constexpr functions(since C++11).
      11) Move constructor is defaulted, and isconstexpr if moving of both elements satisfies the requirements on constexpr functions.

      Contents

      [edit]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
      u -pair-like object 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

      [edit]Exceptions

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

      [edit]Example

      Run this code
      #include <complex>#include <iostream>#include <string>#include <tuple>#include <utility> int main(){auto print=[](auto rem,autoconst& pair){std::cout<< rem<<"("<< pair.first<<", "<< pair.second<<")\n";}; std::pair<int,float> p1;    print("(1) Value-initialized: ", p1); std::pair<int,double> p2{42,3.1415};    print("(2) Initialized with two values: ", p2); std::pair<char,int> p4{p2};    print("(4) Implicitly converted: ", p4); 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')};    print("(8) Piecewise constructed: ", p6);}

      Possible output:

      (1) Value-initialized: (0, 0)(2) Initialized with two values: (42, 3.1415)(4) Implicitly converted: (*, 3)(8) Piecewise constructed: ((0.123,7.7), aaaaaaaaaa)

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 265C++98the default constructor copy-initializedfirst
      andsecond withT1() andT2() respectively
      (thus requiredT1 andT2 to beCopyConstructible)
      first andsecond
      are value-initialized
      LWG 2510C++11the default constructor was implicitmade conditionally-explicit
      N4387C++11some constructors were implicit-only, preventing some usesconstructors made conditionally-explicit

      [edit]See also

      creates apair object of type, determined by the argument types
      (function template)[edit]
      constructs a newtuple
      (public member function ofstd::tuple<Types...>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/pair/pair&oldid=173323"

      [8]ページ先頭

      ©2009-2025 Movatter.jp