BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_addpointer.h
Go to the documentation of this file.
1/// @file bslmf_addpointer.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_addpointer.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ADDPOINTER
9#define INCLUDED_BSLMF_ADDPOINTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_addpointer bslmf_addpointer
15/// @brief Provide meta-function to transform a type to pointer to that type.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_addpointer
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_addpointer-purpose"> Purpose</a>
25/// * <a href="#bslmf_addpointer-classes"> Classes </a>
26/// * <a href="#bslmf_addpointer-description"> Description </a>
27/// * <a href="#bslmf_addpointer-usage"> Usage </a>
28/// * <a href="#bslmf_addpointer-example-1-transform-type-to-pointer-type-to-that-type"> Example 1: Transform Type to Pointer Type to that Type </a>
29///
30/// # Purpose {#bslmf_addpointer-purpose}
31/// Provide meta-function to transform a type to pointer to that type.
32///
33/// # Classes {#bslmf_addpointer-classes}
34///
35/// - bsl::add_pointer: meta-function to transform a type to a pointer type
36/// - bsl::add_pointer_t: alias to the return type of the `bsl::add_pointer`
37///
38/// @see bslmf_removepointer
39///
40/// # Description {#bslmf_addpointer-description}
41/// This component defines a meta-function, `bsl::add_pointer`,
42/// that may be used to transform a type to a pointer to that type.
43///
44/// `bsl::add_pointer` meets the requirements of the @ref add_pointer template
45/// defined in the C++11 standard [meta.trans.ptr].
46///
47/// ## Usage {#bslmf_addpointer-usage}
48///
49///
50/// In this section we show intended use of this component.
51///
52/// ### Example 1: Transform Type to Pointer Type to that Type {#bslmf_addpointer-example-1-transform-type-to-pointer-type-to-that-type}
53///
54///
55/// Suppose that we want to transform a type to a pointer type to that type.
56///
57/// First, we create two `typedef`s -- a pointer type (`MyPtrType`) and the type
58/// pointed to by the pointer type (`MyType`):
59/// @code
60/// typedef int MyType;
61/// typedef int * MyPtrType;
62/// @endcode
63/// Now, we transform `MyType` to a pointer type using `bsl::add_pointer` and
64/// verify that the resulting type is the same as `MyPtrType`:
65/// @code
66/// assert((bsl::is_same<bsl::add_pointer<MyType>::type, MyPtrType>::value));
67/// @endcode
68/// Finally, if the current compiler supports alias templates C++11 feature, we
69/// transform `MyType` to a pointer type using `bsl::add_pointer_t` and verify
70/// that the resulting type is the same as `MyPtrType`:
71/// @code
72/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
73/// assert((bsl::is_same<bsl::add_pointer_t<MyType>, MyPtrType>::value));
74/// #endif
75/// @endcode
76/// Note, that the `bsl::add_pointer_t` avoids the `::type` suffix and
77/// `typename` prefix when we want to use the result of the `bsl::add_pointer`
78/// meta-function in templates.
79/// @}
80/** @} */
81/** @} */
82
83/** @addtogroup bsl
84 * @{
85 */
86/** @addtogroup bslmf
87 * @{
88 */
89/** @addtogroup bslmf_addpointer
90 * @{
91 */
92
93#include <bslscm_version.h>
94
96#include <bsls_platform.h>
97
98#include <stddef.h>
99
100#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
102#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
103
104
105namespace bslmf {
106
107/// This utility `struct` is a private implementation detail that hosts an
108/// overloaded pair of functions that, through SFINAE, can determine whether
109/// it is legal to form a pointer to a specified `t_TYPE`.
110///
111/// See @ref bslmf_addpointer
113
114 struct LargeResult {
115 char d_dummy[99];
116 };
117
118 template <class t_TYPE>
119 static LargeResult canFormPointer(t_TYPE *);
120
121 template <class t_TYPE>
122 static char canFormPointer(...);
123};
124
125/// For the majority of types, it is perfectly reasonable to form the type
126/// `t_TYPE *`.
127///
128/// See @ref bslmf_addpointer
129template <class t_TYPE,
130 size_t = sizeof(AddPointer_Compute::canFormPointer<t_TYPE>(0))>
132
133 typedef t_TYPE *type; // A pointer to the original 't_TYPE'.
134};
135
136/// For special cases, such as references and "abominable" functions, it is
137/// not legal to form a pointer, and this parital specialization will be
138/// chosen by the computed default template parameter.
139template <class t_TYPE>
141
142 typedef t_TYPE type; // Do not modify the type if a pointer is not valid.
143};
144
145#if defined(BSLS_PLATFORM_CMP_IBM)
146template <class t_TYPE>
148 // IBM miscomputes the SFINAE condition for arrays of unknown bound, so we
149 // provide an additional partial specialization for this platform.
150
151 typedef t_TYPE (*type)[]; // A pointer to the original 't_TYPE[]'.
152};
153#endif
154
155} // close package namespace
156
157
158namespace bsl {
159
160 // ==================
161 // struct add_pointer
162 // ==================
163
164/// This `struct` template implements the @ref add_pointer meta-function
165/// defined in the C++11 standard [meta.trans.ptr], providing an alias,
166/// `type`, that returns the result. If the (template parameter) `t_TYPE`
167/// is not a reference type, then `type` is an alias to a pointer type that
168/// points to `t_TYPE`; otherwise, `type` is an alias to a pointer type that
169/// points to the type referred to by the reference `t_TYPE`, unless it is
170/// not legal to form such a pointer type, in which case `type` is an alias
171/// for `t_TYPE`.
172///
173/// See @ref bslmf_addpointer
174template <class t_TYPE>
176
177 /// This `typedef` is an alias to a pointer type that points to the
178 /// (template parameter) `t_TYPE` if it is not a reference type;
179 /// otherwise, this `typedef` is an alias to a pointer type that points
180 /// to the type referred to by the reference `t_TYPE`.
181 typedef typename BloombergLP::bslmf::AddPointer_Impl<t_TYPE>::type type;
182};
183
184/// If we can form a reference to `t_TYPE`, then we can also form a pointer
185/// to it. In particular, we know that it is not `void` (which should still
186/// work) or an "abominable" function type with a trailing cv-qualifier.
187///
188/// \note Note that this partial specialization is necessary to avoid falling into
189/// degenerate (non-compiling) cases in the implementation meta-program.
190template <class t_TYPE>
192
193 typedef t_TYPE * type;
194};
195
196#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
197/// If we can form a reference to `t_TYPE`, then we can also form a pointer
198/// to it. In particular, we know that it is not `void` (which should still
199/// work) or an "abominable" function type with a trailing cv-qualifier.
200///
201/// \note Note that this partial specialization is necessary to avoid falling into
202/// degenerate (non-compiling) cases in the implementation meta-program.
203template <class t_TYPE>
205
206 typedef t_TYPE * type;
207};
208#endif
209
210#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
211
212//ALIASES
213
214/// @ref add_pointer_t is an alias to the return type of the `bsl::add_pointer`
215/// meta-function. Note, that the @ref remove_pointer_t avoids the `::type`
216/// suffix and `typename` prefix when we want to use the result of the
217/// meta-function in templates.
218template <class t_TYPE>
219using add_pointer_t = typename add_pointer<t_TYPE>::type;
220
221#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
222
223} // close namespace bsl
224
225#endif
226
227// ----------------------------------------------------------------------------
228// Copyright 2017 Bloomberg Finance L.P.
229//
230// Licensed under the Apache License, Version 2.0 (the "License");
231// you may not use this file except in compliance with the License.
232// You may obtain a copy of the License at
233//
234// http://www.apache.org/licenses/LICENSE-2.0
235//
236// Unless required by applicable law or agreed to in writing, software
237// distributed under the License is distributed on an "AS IS" BASIS,
238// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
239// See the License for the specific language governing permissions and
240// limitations under the License.
241// ----------------------------- END-OF-FILE ----------------------------------
242
243/** @} */
244/** @} */
245/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
Definition bdlbb_blob.h:579
t_TYPE * type
Definition bslmf_addpointer.h:193
Definition bslmf_addpointer.h:175
BloombergLP::bslmf::AddPointer_Impl< t_TYPE >::type type
Definition bslmf_addpointer.h:181
Definition bslmf_addpointer.h:114
char d_dummy[99]
Definition bslmf_addpointer.h:115
Definition bslmf_addpointer.h:112
static char canFormPointer(...)
static LargeResult canFormPointer(t_TYPE *)
t_TYPE type
Definition bslmf_addpointer.h:142
Definition bslmf_addpointer.h:131
t_TYPE * type
Definition bslmf_addpointer.h:133