BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_decay.h
Go to the documentation of this file.
1/// @file bslmf_decay.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_decay.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_DECAY
9#define INCLUDED_BSLMF_DECAY
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_decay bslmf_decay
15/// @brief Convert a type to the type used for pass-by-value.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_decay
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_decay-purpose"> Purpose</a>
25/// * <a href="#bslmf_decay-classes"> Classes </a>
26/// * <a href="#bslmf_decay-description"> Description </a>
27/// * <a href="#bslmf_decay-usage"> Usage </a>
28/// * <a href="#bslmf_decay-usage-example-1"> Usage Example 1 </a>
29///
30/// # Purpose {#bslmf_decay-purpose}
31/// Convert a type to the type used for pass-by-value.
32///
33/// # Classes {#bslmf_decay-classes}
34///
35/// - bsl::decay: type trait computing return type for type parameter
36/// - bsl::decay_t: alias to the return type of the `bsl::decay` meta-function
37///
38/// @see bslmf_removeextent
39///
40/// # Description {#bslmf_decay-description}
41/// This component provides a metafunction, `bsl::decay`, that
42/// applies array-to-pointer and function-to-pointer conversion and
43/// cv-qualification removal to a type, thus modeling the decay of an argument
44/// type when passed by-value into a function. `bsl::decay` provides identical
45/// functionality to the C++11 standard metafunction `std::decay`. From the
46/// C++14, standard description of `std::decay`:
47///
48/// Let `U` be `remove_reference_t<T>`. If `is_array<U>::value` is `true`, the
49/// member typedef `type` shall equal `remove_extent_t<U>*`. If
50/// `is_function<U>::value` is `true`, the member typedef `type` shall equal
51/// `add_pointer_t<U>`. Otherwise the member typedef `type` equals
52/// `remove_cv_t<U>`. [ *Note*: This behavior is similar to the
53/// lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer
54/// (4.3) conversions applied when an lvalue expression is used as an rvalue,
55/// but also strips cv-qualifiers from class types in order to more closely
56/// model by-value argument passing. - *end note* ]
57///
58/// ## Usage {#bslmf_decay-usage}
59///
60///
61///
62/// ### Usage Example 1 {#bslmf_decay-usage-example-1}
63///
64///
65/// A class template needs to cache a value of type `T`. There is nothing in the
66/// definition of the class that would prevent it from working for `T` of
67/// function type or array-of-unknown bound, but one cannot simply declare a
68/// member of either of those types. Instead, we use `bsl::decay<T>::type`,
69/// which can be stored, copied, and compared as needed:
70/// @code
71/// #ifndef INCLUDED_BSLMF_DECAY
72/// #include <bslmf_decay.h>
73/// #endif
74///
75/// template <class t_TYPE>
76/// class Thing {
77/// public:
78///
79/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
80/// @endcode
81/// Note that if the current compiler supports alias templates C++11 feature, we
82/// can use `bsl::decay_t` alias to the "result" type of the `bsl::decay`
83/// meta-function, that avoids the `::type` suffix and `typename` prefix in the
84/// declaration of the function return type:
85/// @code
86/// using CacheType = bsl::decay_t<t_TYPE>;
87/// #else
88/// typedef typename bsl::decay<t_TYPE>::type CacheType;
89/// #endif
90///
91/// private:
92/// CacheType d_cache;
93/// // ...
94///
95/// public:
96/// CacheType cache() const { return d_cache; }
97/// };
98/// @endcode
99/// Now verify that for function and array types, `cache()` will return a simple
100/// pointer:
101/// @code
102/// int main()
103/// {
104/// typedef const int A1[];
105/// typedef double A2[3][4];
106/// typedef void F1(int);
107/// typedef int (&F2)();
108///
109/// assert((bsl::is_same<const int*, Thing<A1>::CacheType>::value));
110/// assert((bsl::is_same<double(*)[4], Thing<A2>::CacheType>::value));
111/// assert((bsl::is_same<void (*)(int), Thing<F1>::CacheType>::value));
112/// assert((bsl::is_same<int (*)(), Thing<F2>::CacheType>::value));
113///
114/// return 0;
115/// }
116/// @endcode
117/// @}
118/** @} */
119/** @} */
120
121/** @addtogroup bsl
122 * @{
123 */
124/** @addtogroup bslmf
125 * @{
126 */
127/** @addtogroup bslmf_decay
128 * @{
129 */
130
131#include <bslscm_version.h>
132
133#include <bslmf_addpointer.h>
134#include <bslmf_assert.h>
135#include <bslmf_isarray.h>
136#include <bslmf_isfunction.h>
137#include <bslmf_removecv.h>
138#include <bslmf_removeextent.h>
140
142
143namespace bsl {
144
145// Forward declaration
146template <class t_TYPE, bool t_IS_ARRAY, bool t_IS_FUNC> struct decay_imp;
147
148 // ====================
149 // class template decay
150 // ====================
151
152/// Metafunction to return the variant of the specified parameter `t_TYPE`
153/// used for pass-by-reference, e.g., by applying array-to-pointer and
154/// function-to-pointer conversion.
155///
156/// See @ref bslmf_decay
157template <class t_TYPE>
158class decay {
159
160 typedef typename bsl::remove_reference<t_TYPE>::type U;
161 enum {
162 k_ISARRAY = is_array<U>::value,
163 k_ISFUNC = is_function<U>::value
164 };
165
166 public:
168};
169
170#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
171
172// ALIASES
173
174/// `decay_t` is an alias to the return type of the `bsl::decay`
175/// meta-function. Note, that the @ref enable_if_t avoids the `::type` suffix
176/// and `typename` prefix when we want to use the result of the
177/// meta-function in templates.
178template <class t_TYPE>
179using decay_t = typename decay<t_TYPE>::type;
180
181#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
182
183// ============================================================================
184// CLASS TEMPLATE IMPLEMENTATION
185// ============================================================================
186
187template <class t_TYPE, bool t_IS_ARRAY, bool t_IS_FUNC>
188struct decay_imp : remove_cv<t_TYPE> {
189 BSLMF_ASSERT(!(t_IS_ARRAY || t_IS_FUNC));
190};
191
192template <class t_TYPE>
193struct decay_imp<t_TYPE, true /* t_IS_ARRAY */, false /* t_IS_FUNC */> {
195};
196
197template <class t_TYPE>
198struct decay_imp<t_TYPE, false /* t_IS_ARRAY */, true /* t_IS_FUNC */> {
200};
201
202} // close namespace bsl
203
204#endif // ! defined(INCLUDED_BSLMF_DECAY)
205
206// ----------------------------------------------------------------------------
207// Copyright 2016 Bloomberg Finance L.P.
208//
209// Licensed under the Apache License, Version 2.0 (the "License");
210// you may not use this file except in compliance with the License.
211// You may obtain a copy of the License at
212//
213// http://www.apache.org/licenses/LICENSE-2.0
214//
215// Unless required by applicable law or agreed to in writing, software
216// distributed under the License is distributed on an "AS IS" BASIS,
217// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
218// See the License for the specific language governing permissions and
219// limitations under the License.
220// ----------------------------- END-OF-FILE ----------------------------------
221
222/** @} */
223/** @} */
224/** @} */
Definition bslmf_decay.h:158
decay_imp< U, k_ISARRAY, k_ISFUNC >::type type
Definition bslmf_decay.h:167
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
BloombergLP::bslmf::AddPointer_Impl< t_TYPE >::type type
Definition bslmf_addpointer.h:181
add_pointer< t_TYPE >::type type
Definition bslmf_decay.h:199
remove_extent< t_TYPE >::type * type
Definition bslmf_decay.h:194
Definition bslmf_decay.h:188
BSLMF_ASSERT(!(t_IS_ARRAY||t_IS_FUNC))
Definition bslmf_isarray.h:168
Definition bslmf_isfunction.h:232
Definition bslmf_removecv.h:120
remove_const< typenameremove_volatile< t_TYPE >::type >::type type
Definition bslmf_removecv.h:128
t_TYPE type
Definition bslmf_removeextent.h:134
t_TYPE type
This typedef is an alias to the (template parameter) t_TYPE.
Definition bslmf_removereference.h:156