1 概述
in_place系列,不是占位符,而是一个类型标记,用于指示如何解析和处理参数。
它的存在,有助于消除歧义,正确地使用参数。
2 作用
1 in_place_t
指示这些参数用来构造对象
struct in_place_t {
explicit in_place_t() = default;
};
inline constexpr in_place_t in_place{};
比如:optional库
template<typename... _Args,
_Requires<is_constructible<_Tp, _Args...>> = false>
explicit constexpr
optional(in_place_t, _Args&&... __args)
noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
: _Base(std::in_place, std::forward<_Args>(__args)...) { }
2 in_place_type_t
指示这些参数用来构造哪一个类型的对象
template<typename _Tp> struct in_place_type_t
{
explicit in_place_type_t() = default;
};
template<typename _Tp>
inline constexpr in_place_type_t<_Tp> in_place_type{};
例如:variant库中的
指示 _args 这些参数用于构造类型为_Tp的对象
constexpr explicit
variant(in_place_type_t<_Tp>, _Args&&... __args)
: variant(in_place_index<__index_of<_Tp>>,
std::forward<_Args>(__args)...)
{ }
3 in_place_index_t
指示这些参数构造哪一个下标的对象
template<size_t _Idx> struct in_place_index_t
{
explicit in_place_index_t() = default;
};
template<size_t _Idx>
inline constexpr in_place_index_t<_Idx> in_place_index{};
例如:variant库中
指示这些参数用于构造数字索引为 _Np类型的对象
constexpr explicit
variant(in_place_index_t<_Np>, _Args&&... __args)
: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
_Default_ctor_enabler(_Enable_default_constructor_tag{})
{ }