utility_in_place
utility_in_place (제자리 생성 태그)
이 페이지는 C++17부터 도입된 std::in_place, std::in_place_type, std::in_place_index 및 그에 대응하는 타입들에 대해 다루고 있어요. 이들은 std::optional, std::variant, std::any 같은 타입의 생성자에서 어떤 인자로 초기화할지 명확하게 지정하는 데 사용하는 태그(tag)예요. 즉, 임시 객체를 복사하거나 이동하는 대신 그 자리에서 직접 생성하고 싶을 때 사용해요.
출처: cppreference
본문
<utility> 헤더에 정의됨:
| 정의 | 번호 | 도입 |
|---|---|---|
struct in_place_t { explicit in_place_t () = default ; }; |
(1) | (since C++17) |
inline constexpr std :: in_place_t in_place {}; |
(2) | (since C++17) |
template < class T > struct in_place_type_t { explicit in_place_type_t () = default ; }; |
(3) | (since C++17) |
template < class T > constexpr std :: in_place_type_t < T > in_place_type {}; |
(4) | (since C++17) |
template < std :: size_t I > struct in_place_index_t { explicit in_place_index_t () = default ; }; |
(5) | (since C++17) |
template < std :: size_t I > constexpr std :: in_place_index_t < I > in_place_index {}; |
(6) | (since C++17) |
이 태그들은 생성자 오버로드 해석을 명확하게 해 주는 역할을 해요. 예를 들어 std::optional을 생성할 때 std::in_place를 전달하면, 생성자 인자를 직접 전달하여 객체를 제자리에서 생성할 수 있어요. std::in_place_type은 타입을 명시적으로 지정해야 할 때, std::in_place_index는 변형(variant)에서 인덱스로 대안을 선택해야 할 때 사용해요.
표준 라이브러리
다음 표준 라이브러리 타입들이 (1-6)을 모호성 제거 태그로 사용해요:
| 타입 | 설명 |
|---|---|
| any (C++17) | 모든 CopyConstructible 타입의 인스턴스를 보관하는 객체 (클래스) [편집] |
| expected (C++23) | 기대값 또는 오류값 중 하나를 담는 래퍼 (클래스 템플릿) [편집] |
| move_only_function (C++23) | 주어진 호출 시그니처에서 한정자(qualifier)를 지원하는 임의의 호출 가능 객체를 위한 이동 전용 래퍼 (클래스 템플릿) [편집] |
| optional (C++17) | 객체를 보관할 수도 있고 보관하지 않을 수도 있는 래퍼 (클래스 템플릿) [편집] |
| variant (C++17) | 타입 안전한 구분 공용체(discriminated union) (클래스 템플릿) [편집] |
같이 보기
| 태그 | 설명 |
|---|---|
| sorted_unique sorted_unique_t (C++23) | 범위의 요소들이 정렬되어 있고 유일함을 나타냄 (태그) [편집] |
| sorted_equivalent sorted_equivalent_t (C++23) | 범위의 요소들이 정렬되어 있음을 나타냄 (유일성은 필요하지 않음) (태그) [편집] |