std::in_place, std::in_place_type, std::in_place_index, std::in_place_t, std::in_place_type_t, std::in_place_index_t (C++17)

(Also see https://www.cppstories.com/2018/07/in-place-cpp17/)

Defined in header <utility>.

These helpers are used to efficiently initialise objects in-place - without additional temporary copy or move operations.


The following standard library types use (1-6) as disambiguation tags:

See variant constructors taking a in_place* parameter.

Syntax

struct in_place_t { explicit in_place_t() = default; }; // (1) for std::optional
inline constexpr std::in_place_t in_place {}; // (2) for std::optional
template< class T >
struct in_place_type_t { explicit in_place_type_t() = default; }; // (3) used for std::variant and std::any
template< class T >
constexpr std::in_place_type_t<T> in_place_type {}; // (4) used for std::variant and std::any
template< std::size_t I >
struct in_place_index_t { explicit in_place_index_t() = default; }; // (5) used for std::variant
template< std::size_t I >
constexpr std::in_place_index_t<I> in_place_index {}; // (6) used for std::variant

1,3,5) The type/type templates std::in_place_t, std::in_place_type_t and std::in_place_index_t can be used in the constructor's parameter list to match the intended tag.

2,4,6) The corresponding std::in_place, std::in_place_type, and std::in_place_index instances of (1,3,5) are disambiguation tags that can be passed to the constructors to indicate that the contained object should be constructed in-place, and (for the latter two) the type of the object to be constructed.