Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/in place"

From cppreference.com
< cpp‎ | utility
(See also: rm)
m (rm `inline` in `template<...> inline constexpr`; todo: update <utility> too)
 
(16 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/title|in_place|in_place_type|in_place_index|in_place_t|in_place_type_t|in_place_index_t }}
+
{{cpp/title|in_place|in_place_type|in_place_index|in_place_t|in_place_type_t|in_place_index_t}}
 
{{cpp/utility/navbar}}
 
{{cpp/utility/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | utility}}
+
{{dcl header|utility}}
{{dcl | since=c++17 | 1=
+
{{dcl|num=1|since=c++17|1=
struct in_place_t {
+
struct in_place_t { explicit in_place_t() = default; };
    explicit in_place_t() = default;
+
};
+
inline constexpr std::in_place_t in_place{};
+
 
}}
 
}}
{{dcl | since=c++17 | 1=
+
{{dcl|num=2|since=c++17|1=
template <class T> struct in_place_type_t {
+
inline constexpr std::in_place_t in_place {};
    explicit in_place_type_t() = default;
+
};
+
template <class T>
+
inline constexpr std::in_place_type_t<T> in_place_type{};
+
 
}}
 
}}
{{dcl | since=c++17 |1=
+
{{dcl|num=3|since=c++17|1=
template <size_t I> struct in_place_index_t {
+
template< class T >
    explicit in_place_index_t() = default;
+
struct in_place_type_t { explicit in_place_type_t() = default; };
};
+
}}
template <size_t I>
+
{{dcl|num=4|since=c++17|1=
inline constexpr in_place_index_t<I> in_place_index{};
+
template< class T >
 +
constexpr std::in_place_type_t<T> in_place_type {};
 +
}}
 +
{{dcl|num=5|since=c++17|1=
 +
template< std::size_t I >
 +
struct in_place_index_t { explicit in_place_index_t() = default; };
 +
}}
 +
{{dcl|num=6|since=c++17|1=
 +
template< std::size_t I >
 +
constexpr std::in_place_index_t<I> in_place_index {};
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
{{tt|std::in_place}}, {{tt|std::in_place_type}}, and {{tt|std::in_place_index}} are disambiguation tags that can be passed to the constructors of {{lc|std::optional}}, {{lc|std::variant}}, and {{lc|std::any}} to indicate that the contained object should be constructed in-place, and (for the latter two) the type of the object to be constructed.  
+
@1,3,5@ The type/type templates {{tt|std::in_place_t}}, {{tt|std::in_place_type_t}} and {{tt|std::in_place_index_t}} can be used in the constructor's parameter list to match the intended tag.
  
The corresponding type/type templates {{tt|std::in_place_t}}, {{tt|std::in_place_type_t}} and {{tt|std::in_place_index_t}} can be used in the constructor's parameter list to match the intended tag.
+
@2,4,6@ The corresponding {{tt|std::in_place}}, {{tt|std::in_place_type}}, and {{tt|std::in_place_index}} instances of {{v|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.
 +
 
 +
===Standard library===
 +
The following standard library types use {{v|1-6}} as disambiguation tags:
 +
{{dsc begin}}
 +
{{dsc inc|cpp/utility/dsc any}}
 +
{{dsc inc|cpp/utility/dsc expected}}
 +
{{dsc inc|cpp/utility/functional/dsc move_only_function}}
 +
{{dsc inc|cpp/utility/dsc optional}}
 +
{{dsc inc|cpp/utility/dsc variant}}
 +
{{dsc end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/dsc optional}}
+
{{dsc inc|cpp/container/dsc sorted_unique}}
{{dsc inc | cpp/utility/dsc variant}}
+
{{dsc inc|cpp/container/dsc sorted_equivalent}}
{{dsc inc | cpp/utility/dsc any}}
+
 
{{dsc end}}
 
{{dsc end}}
 +
 +
{{langlinks|ja|ru|zh}}

Latest revision as of 02:57, 24 September 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Defined in header <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)
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.

[edit] Standard library

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

(C++17)
objects that hold instances of any CopyConstructible type
(class) [edit]
(C++23)
a wrapper that contains either an expected or error value
(class template) [edit]
move-only wrapper of any callable object that supports qualifiers in a given call signature
(class template) [edit]
(C++17)
a wrapper that may or may not hold an object
(class template) [edit]
(C++17)
a type-safe discriminated union
(class template) [edit]

[edit] See also

indicates that elements of a range are sorted and unique
(tag)[edit]
indicates that elements of a range are sorted (uniqueness is not required)
(tag)[edit]