std::pair<T1,T2>::pair
pair();
|
(1) | (C++11부터 constexpr) (C++11부터 조건부 explicit) |
pair( const T1& x, const T2& y );
|
(2) | (C++14부터 constexpr) (C++11부터 조건부 explicit) |
template< class U1, class U2 >
pair( U1&& x, U2&& y );
|
(3) | (C++11부터) (C++14부터 constexpr) (C++23까지) (조건부 explicit) |
template< class U1 = T1, class U2 = T2 >
constexpr pair( U1&& x, U2&& y );
|
(C++23부터) (조건부 explicit) |
|
template< class U1, class U2 >
constexpr pair( pair<U1, U2>& p );
|
(4) | (C++23부터) (조건부 explicit) |
template< class U1, class U2 >
pair( const pair<U1, U2>& p );
|
(5) | (C++14부터 constexpr) (C++11부터 조건부 explicit) |
template< class U1, class U2 >
pair( pair<U1, U2>&& p );
|
(6) | (C++14부터 constexpr) (C++11부터 조건부 explicit) |
template< class U1, class U2 >
constexpr pair( const pair<U1, U2>&& p );
|
(7) | (C++23부터) (조건부 explicit) |
template< pair-like P >
constexpr pair ( P&& u );
|
(8) | (C++23부터) (조건부 explicit) |
template< class... Args1, class... Args2 >
pair( std::piecewise_construct_t,
std::tuple<Args1...> first_args,
std::tuple<Args2...> second_args );
|
(9) | (C++11부터) (C++20부터 constexpr) |
pair( const pair& p ) = default;
|
(10) | |
pair( pair&& p ) = default;
|
(11) | (C++11부터) |
새 pair를 생성합니다.
first 및 second.
|
이 생성자는 이 생성자는 |
(C++11부터) |
first를 x로, second을 y로 초기화합니다.
|
이 생성자는 이 생성자는 |
(C++11부터) |
first를 std::forward<U1>(x)로, second을 std::forward<U2>(y)로 초기화합니다.std::is_constructible_v<T1, U1> 및 std::is_constructible_v<T2, U2>이 모두 true인 경우에만 오버로드 해결에 참여합니다.explicit이 std::is_convertible_v<U1, T1>이거나 false 또는 std::is_convertible_v<U2, T2>이 false인 경우에만 |
이 생성자는 |
(C++23부터) |
first를 p.first로, second을 p.second로 초기화합니다.std::is_constructible_v<T1, U1&> 및 std::is_constructible_v<T2, U2&>이 모두 true인 경우에만 오버로드 해결에 참여합니다.explicit이 std::is_convertible_v<U1&, T1>이거나 false 또는 std::is_convertible_v<U2&, T2>이 false인 경우에만 first 또는 second의 초기화가 임시 객체에 참조를 바인딩하는 경우 삭제된 것으로 정의됩니다.first를 p.first로, second을 p.second로 초기화합니다.
|
이 생성자는 이 생성자는 |
(C++11부터) |
|
이 생성자는 |
(C++23부터) |
first를 std::forward<U1>(p.first)로, second을 std::forward<U2>(p.second)로 초기화합니다.std::is_constructible_v<T1, U1> 및 std::is_constructible_v<T2, U2>이 모두 true인 경우에만 오버로드 해결에 참여합니다.explicit이 std::is_convertible_v<U1, T1>이거나 false 또는 std::is_convertible_v<U2, T2>이 false인 경우에만 |
이 생성자는 |
(C++23부터) |
first를 std::forward<const U1>(p.first)로, second을 std::forward<const U2>(p.second)로 초기화합니다.std::is_constructible_v<T1, U1> 및 std::is_constructible_v<T2, U2>이 모두 true인 경우에만 오버로드 해결에 참여합니다.explicit이 std::is_convertible_v<const U1, T1>이거나 false 또는 std::is_convertible_v<const U2, T2>이 false인 경우에만 first 또는 second의 초기화가 임시 객체에 참조를 바인딩하는 경우 삭제된 것으로 정의됩니다.u1를 std::get<0>(std::forward(u))로, u2을 std::get<1>(std::forward(u))로 지정하고, 해당 타입을 각각 U1 및 U2로 표기합니다. first를 u1로, second를 u2로 초기화합니다.std::remove_cvref(P)이 std::ranges::subrange의 특수화가 아니고,std::is_constructible_v<T1, U1>이true이며, 그리고std::is_constructible_v<T2, U2이true입니다.
explicit이 std::is_convertible_v<U1, T1>이거나 false 또는 std::is_convertible_v<U2, T2>가 false인 경우에만 first 또는 second의 초기화가 임시 객체에 참조를 바인딩하는 경우 삭제된 것으로 정의됩니다.first_args의 요소들을 first의 생성자로 전달하고 second_args의 요소들을 second의 생성자로 전달합니다. 이는 복사 불가능하고 이동 불가능한 타입의 pair를 생성하는 데 사용할 수 있는 유일한 비기본 생성자입니다. first 또는 second이 참조이고 임시 객체에 바인딩된 경우 프로그램은 ill-formed입니다.constexpr입니다(C++11부터).constexpr입니다.매개변수
| x | - | 이 pair의 첫 번째 요소를 초기화하는 값 |
| y | - | 이 pair의 두 번째 요소를 초기화하는 값 |
| p | - | 이 pair의 두 요소를 모두 초기화하는 데 사용되는 값들의 pair |
| u | - | pair-like 이 pair의 두 요소를 모두 초기화하는 데 사용되는 값들의 pair-like 객체 |
| first_args | - | 이 pair의 첫 번째 요소를 초기화하는 생성자 인수들의 tuple |
| second_args | - | 이 pair의 두 번째 요소를 초기화하는 생성자 인수들의 tuple |
예외
지정된 연산(예: 요소의 생성자) 중 하나가 예외를 던지지 않는 한 예외를 던지지 않습니다.
예제
#include <complex> #include <iostream> #include <string> #include <tuple> #include <utility> int main() { auto print = [](auto rem, auto const& 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); }
가능한 출력:
(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)
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 265 | C++98 |
기본 생성자가
first
와
second
를 각각
T1
(
)
와
T2
(
)
로 복사 초기화함
(따라서
T1
과
T2
가
CopyConstructible
여야 함)
|
first
와
second
가 값 초기화됨 |
| LWG 2510 | C++11 | 기본 생성자가 암시적이었음 | 조건부 명시적으로 변경됨 |
| N4387 | C++11 | 일부 생성자가 암시적 전용이어서 특정 사용을 방해함 | 생성자들이 조건부 명시적으로 변경됨 |
참고 항목
인자 타입에 의해 결정된 타입의
pair
객체를 생성함
(함수 템플릿) |
|
새로운
tuple
을 생성함
(
std::tuple<Types...>
의 public 멤버 함수)
|