BDE 4.14.0 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_assert.h>
134#include <bslmf_isarray.h>
135#include <bslmf_isfunction.h>
136#include <bslmf_removecv.h>
137#include <bslmf_removeextent.h>
139
141
142namespace bsl {
143
144// Forward declaration
145template <class t_TYPE, bool t_IS_ARRAY, bool t_IS_FUNC> struct decay_imp;
146
147 // ====================
148 // class template decay
149 // ====================
150
151/// Metafunction to return the variant of the specified parameter `t_TYPE`
152/// used for pass-by-reference, e.g., by applying array-to-pointer and
153/// function-to-pointer conversion.
154///
155/// See @ref bslmf_decay
156template <class t_TYPE>
157class decay {
158
159 typedef typename bsl::remove_reference<t_TYPE>::type U;
160 enum {
161 k_ISARRAY = is_array<U>::value,
162 k_ISFUNC = is_function<U>::value
163 };
164
165 public:
167};
168
169#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
170
171// ALIASES
172
173/// `decay_t` is an alias to the return type of the `bsl::decay`
174/// meta-function. Note, that the `enable_if_t` avoids the `::type` suffix
175/// and `typename` prefix when we want to use the result of the
176/// meta-function in templates.
177template <class t_TYPE>
178using decay_t = typename decay<t_TYPE>::type;
179
180#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
181
182// ============================================================================
183// CLASS TEMPLATE IMPLEMENTATION
184// ============================================================================
185
186template <class t_TYPE, bool t_IS_ARRAY, bool t_IS_FUNC>
187struct decay_imp : remove_cv<t_TYPE> {
188 BSLMF_ASSERT(!(t_IS_ARRAY || t_IS_FUNC));
189};
190
191template <class t_TYPE>
192struct decay_imp<t_TYPE, true /* t_IS_ARRAY */, false /* t_IS_FUNC */> {
194};
195
196template <class t_TYPE>
197struct decay_imp<t_TYPE, false /* t_IS_ARRAY */, true /* t_IS_FUNC */> {
198 typedef t_TYPE *type;
199};
200
201} // close namespace bsl
202
203#endif // ! defined(INCLUDED_BSLMF_DECAY)
204
205// ----------------------------------------------------------------------------
206// Copyright 2016 Bloomberg Finance L.P.
207//
208// Licensed under the Apache License, Version 2.0 (the "License");
209// you may not use this file except in compliance with the License.
210// You may obtain a copy of the License at
211//
212// http://www.apache.org/licenses/LICENSE-2.0
213//
214// Unless required by applicable law or agreed to in writing, software
215// distributed under the License is distributed on an "AS IS" BASIS,
216// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217// See the License for the specific language governing permissions and
218// limitations under the License.
219// ----------------------------- END-OF-FILE ----------------------------------
220
221/** @} */
222/** @} */
223/** @} */
Definition bslmf_decay.h:157
decay_imp< U, k_ISARRAY, k_ISFUNC >::type type
Definition bslmf_decay.h:166
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_printmethods.h:283
t_TYPE * type
Definition bslmf_decay.h:198
remove_extent< t_TYPE >::type * type
Definition bslmf_decay.h:193
Definition bslmf_decay.h:187
BSLMF_ASSERT(!(t_IS_ARRAY||t_IS_FUNC))
Definition bslmf_isarray.h:168
Definition bslmf_isfunction.h:232
Definition bslmf_removecv.h:118
remove_const< typenameremove_volatile< t_TYPE >::type >::type type
Definition bslmf_removecv.h:126
t_TYPE type
Definition bslmf_removeextent.h:132