Namespaces
Variants

std::piecewise_construct, std::piecewise_construct_t

cppreference.net에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐기됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
Swap 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
헤더 <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는 (1)의 인스턴스입니다.

std::piecewise_construct_t을 사용하지 않는 오버로드는 각 튜플 인수가 pair의 요소가 된다고 가정합니다. std::piecewise_construct_t을 사용하는 오버로드는 각 튜플 인수가 지정된 타입의 새 객체를 조각별(piecewise)로 생성하는 데 사용되며, 이 객체가 pair의 요소가 된다고 가정합니다.

표준 라이브러리

다음 표준 라이브러리 타입과 함수들은 이를 모호성 제거 태그로 사용합니다:

이진 튜플, 즉 값의 쌍을 구현함
(클래스 템플릿)
주어진 타입에 필요한 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 멤버 함수)