BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_unwrapreference.h
Go to the documentation of this file.
1/// @file bslmf_unwrapreference.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_unwrapreference.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_UNWRAPREFERENCE
9#define INCLUDED_BSLMF_UNWRAPREFERENCE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_unwrapreference bslmf_unwrapreference
15/// @brief Provide a meta-function to unwrap reference wrappers.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_unwrapreference
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_unwrapreference-purpose"> Purpose</a>
25/// * <a href="#bslmf_unwrapreference-classes"> Classes </a>
26/// * <a href="#bslmf_unwrapreference-canonical-header"> Canonical Header </a>
27/// * <a href="#bslmf_unwrapreference-description"> Description </a>
28/// * <a href="#bslmf_unwrapreference-usage"> Usage </a>
29/// * <a href="#bslmf_unwrapreference-example-1-unwrap-reference-wrapped-types"> Example 1: Unwrap Reference Wrapped Types </a>
30///
31/// # Purpose {#bslmf_unwrapreference-purpose}
32/// Provide a meta-function to unwrap reference wrappers.
33///
34/// # Classes {#bslmf_unwrapreference-classes}
35///
36/// - bsl::unwrap_reference: standard meta-function to unwrap reference wrappers
37/// - bsl::unwrap_reference_t: alias to the return type of the meta-function
38///
39/// # Canonical Header {#bslmf_unwrapreference-canonical-header}
40/// bsl_functional.h, bsl_type_traits.h
41///
42/// @see bslmf_referencewrapper, bslmf_unwrap_ref_decay
43///
44/// # Description {#bslmf_unwrapreference-description}
45/// This component defines a meta-function `bsl::unwrap_reference`
46/// that may be used to unwrap a `bsl::reference_wrapper` (which is an alias of
47/// `std::reference_wrapper` when that exists) of some type `U`, resulting in
48/// `U&`. In case the specified type template argument is not a specialization
49/// of `bsl::reference_wrapper` the result is the type itself.
50///
51/// `bsl::unwrap_reference` meets the requirements of the @ref unwrap_reference
52/// template defined in the C++20 standard [meta.trans.other].
53///
54/// ## Usage {#bslmf_unwrapreference-usage}
55///
56///
57/// In this section we show intended use of this component.
58///
59/// ### Example 1: Unwrap Reference Wrapped Types {#bslmf_unwrapreference-example-1-unwrap-reference-wrapped-types}
60///
61///
62/// Suppose that we work in a programming environment where types may be
63/// presented wrapped in a `bsl::reference_wrapper` or its `std` equivalent. We
64/// would like to use a reference to the wrapped type, but use unwrapped types
65/// as they are. This is the exact use case for `bsl::unwrap_reference`.
66///
67/// First, we create types that represent both reference-wrapped, and normal
68/// type parameters:
69/// @code
70/// typedef bsl::reference_wrapper<int *> WrappedType;
71/// typedef int *NotWrappedType;
72/// @endcode
73/// Next, we create types that are references if they were wrapped:
74/// @code
75/// typedef bsl::unwrap_reference<WrappedType>::type UnwrappedWrapped;
76/// typedef bsl::unwrap_reference<NotWrappedType>::type UnwrappedNotWrapped;
77/// @endcode
78/// Finally we can verify that the wrapped type became a reference, while the
79/// other type is unchanged:
80/// @code
81/// assert((true == bsl::is_same<UnwrappedWrapped, int *&>::value));
82/// assert((true == bsl::is_same<UnwrappedNotWrapped, int *>::value));
83/// @endcode
84/// Note, that (when available) the `bsl::unwrap_reference_t` avoids the
85/// `::type` suffix and `typename` prefix when we want to use the result of the
86/// `bsl::unwrap_reference` meta-function in templates.
87/// @}
88/** @} */
89/** @} */
90
91/** @addtogroup bsl
92 * @{
93 */
94/** @addtogroup bslmf
95 * @{
96 */
97/** @addtogroup bslmf_unwrapreference
98 * @{
99 */
100
101#include <bslscm_version.h>
102
104
106#include <bsls_libraryfeatures.h>
107
108#if BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
109 #include <type_traits> // 'std::unwrap_reference', 'std::unwrap_reference_t'
110#endif
111
112 // =======================
113 // struct unwrap_reference
114 // =======================
115
116namespace bsl {
117
118#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
119using std::unwrap_reference;
120using std::unwrap_reference_t;
121#else
122
123/// This `struct` template implements the @ref unwrap_reference meta-function
124/// defined in the C++20 standard [meta.trans.other], providing an alias,
125/// `type`, that returns the result. `type` denotes the same type as the
126/// (template parameter) `t_TYPE` unless `t_TYPE` is a specialization of
127/// `bsl::reference_wrapper` (which is an alias of `std::reference_wrapper<U>`
128/// if that exists) for a type `U`, in which case `type` is `U&`.
129///
130/// See @ref bslmf_unwrapreference
131template <class t_TYPE>
132struct unwrap_reference {
133 // TYPES
134
135 /// This `typedef` is an alias to the template parameter `t_TYPE`.
136 typedef t_TYPE type;
137};
138
139/// This partial specialization is chosen when the template parameter `t_TYPE`
140/// is a `bsl::reference_wrapper`.
141template <class t_WRAPPED_TYPE>
142struct unwrap_reference<bsl::reference_wrapper<t_WRAPPED_TYPE> > {
143 // TYPES
144
145 /// This `typedef` is an alias to the template parameter `t_WRAPPED_TYPE&`.
146 typedef t_WRAPPED_TYPE& type;
147};
148
149#if defined(BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES)
150// ALIASES
151
152/// @ref unwrap_reference_t is an alias to the return type of the
153/// `bsl::unwrap_reference` meta-function.
154template <class t_TYPE>
155using unwrap_reference_t = typename unwrap_reference<t_TYPE>::type;
156#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
157
158#endif // else of BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
159
160} // close namespace bsl
161
162#endif
163
164// ----------------------------------------------------------------------------
165// Copyright 2024 Bloomberg Finance L.P.
166//
167// Licensed under the Apache License, Version 2.0 (the "License");
168// you may not use this file except in compliance with the License.
169// You may obtain a copy of the License at
170//
171// http://www.apache.org/licenses/LICENSE-2.0
172//
173// Unless required by applicable law or agreed to in writing, software
174// distributed under the License is distributed on an "AS IS" BASIS,
175// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
176// See the License for the specific language governing permissions and
177// limitations under the License.
178// ----------------------------- END-OF-FILE ----------------------------------
179
180/** @} */
181/** @} */
182/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939