std::piecewise_construct, std::piecewise_construct_t
cppreference.com에서
| 헤더 <utility>
|
||
struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
|
에 정의됨 (1) | (C++11부터) |
constexpr std::piecewise_construct_t piecewise_construct{};
|
(2) | (C++11부터) (C++17부터 인라인) |
1)
std::piecewise_construct_t 은 두 개의 튜플 인자를 받는 서로 다른 함수들을 구별하는 데 사용되는 빈 클래스 태그 타입입니다.2) 상수
std::piecewise_construct 는 의 인스턴스입니다.std::piecewise_construct_t 를 사용하지 않는 오버로드는 각 튜플 인자가 쌍의 한 요소가 된다고 가정합니다. std::piecewise_construct_t 를 사용하는 오버로드는 각 튜플 인자가 지정된 타입의 새 객체를 조각별(piecewise)로 생성하는 데 사용되며, 해당 객체가 쌍의 요소가 된다고 가정합니다.
표준 라이브러리
다음 표준 라이브러리 타입과 함수들은 이를 모호성 제거 태그로 사용합니다:
|
이진 튜플, 즉 값의 쌍을 구현함
(클래스 템플릿) |
|
|
(C++20)
|
주어진 타입에 필요한 uses-allocator 생성 방식에 맞는 인수 목록을 준비함
(함수 템플릿) |
동일한 값을 반복적으로 생성하여 만들어진 시퀀스로 구성된
view
(클래스 템플릿) (커스터마이제이션 포인트 객체) |
예제
이 코드 실행
#include <iostream> #include <tuple> #include <utility> struct Foo { Foo(std::tuple<int, float>) { std::cout << "Constructed a Foo from a tuple\n"; } Foo(int, float) { std::cout << "Constructed a Foo from an int and a float\n"; } }; int main() { std::tuple<int, float> t(1, 3.14); std::cout << "Creating p1...\n"; std::pair<Foo, Foo> p1(t, t); std::cout << "Creating p2...\n"; std::pair<Foo, Foo> p2(std::piecewise_construct, t, t); }
출력:
Creating p1... Constructed a Foo from a tuple Constructed a Foo from a tuple Creating p2... Constructed a Foo from an int and a float Constructed a Foo from an int and a float
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2510 | C++11 | 기본 생성자가 non-explicit였으며, 이는 모호성을 초래할 수 있었음 | explicit로 변경됨 |
참고 항목
|
새로운 pair를 생성
(
std::pair<T1,T2>
의 public 멤버 함수)
|