BDE 4.14.0 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`.
111
112 struct LargeResult {
113 char d_dummy[99];
114 };
115
116 template <class t_TYPE>
117 static LargeResult canFormPointer(t_TYPE *);
118
119 template <class t_TYPE>
120 static char canFormPointer(...);
121};
122
123/// For the majority of types, it is perfectly reasonable to form the type
124/// `t_TYPE *`.
125template <class t_TYPE,
126 size_t = sizeof(AddPointer_Compute::canFormPointer<t_TYPE>(0))>
128
129 typedef t_TYPE *type; // A pointer to the original 't_TYPE'.
130};
131
132/// For special cases, such as references and "abominable" functions, it is
133/// not legal to form a pointer, and this parital specialization will be
134/// chosen by the computed default template parameter.
135template <class t_TYPE>
137
138 typedef t_TYPE type; // Do not modify the type if a pointer is not valid.
139};
140
141#if defined(BSLS_PLATFORM_CMP_IBM)
142template <class t_TYPE>
144 // IBM miscomputes the SFINAE condition for arrays of unknown bound, so we
145 // provide an additional partial specialization for this platform.
146
147 typedef t_TYPE (*type)[]; // A pointer to the original 't_TYPE[]'.
148};
149#endif
150
151} // close package namespace
152
153
154namespace bsl {
155
156 // ==================
157 // struct add_pointer
158 // ==================
159
160/// This `struct` template implements the @ref add_pointer meta-function
161/// defined in the C++11 standard [meta.trans.ptr], providing an alias,
162/// `type`, that returns the result. If the (template parameter) `t_TYPE`
163/// is not a reference type, then `type` is an alias to a pointer type that
164/// points to `t_TYPE`; otherwise, `type` is an alias to a pointer type that
165/// points to the type referred to by the reference `t_TYPE`, unless it is
166/// not legal to form such a pointer type, in which case `type` is an alias
167/// for `t_TYPE`.
168template <class t_TYPE>
170
171 /// This `typedef` is an alias to a pointer type that points to the
172 /// (template parameter) `t_TYPE` if it is not a reference type;
173 /// otherwise, this `typedef` is an alias to a pointer type that points
174 /// to the type referred to by the reference `t_TYPE`.
175 typedef typename BloombergLP::bslmf::AddPointer_Impl<t_TYPE>::type type;
176};
177
178/// If we can form a reference to `t_TYPE`, then we can also form a pointer
179/// to it. In particular, we know that it is not `void` (which should still
180/// work) or an "abominable" function type with a trailing cv-qualifier.
181/// Note that this partial specialization is necessary to avoid falling into
182/// degenerate (non-compiling) cases in the implementation meta-program.
183template <class t_TYPE>
185
186 typedef t_TYPE * type;
187};
188
189#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
190/// If we can form a reference to `t_TYPE`, then we can also form a pointer
191/// to it. In particular, we know that it is not `void` (which should still
192/// work) or an "abominable" function type with a trailing cv-qualifier.
193/// Note that this partial specialization is necessary to avoid falling into
194/// degenerate (non-compiling) cases in the implementation meta-program.
195template <class t_TYPE>
197
198 typedef t_TYPE * type;
199};
200#endif
201
202#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
203
204//ALIASES
205
206/// `add_pointer_t` is an alias to the return type of the `bsl::add_pointer`
207/// meta-function. Note, that the `remove_pointer_t` avoids the `::type`
208/// suffix and `typename` prefix when we want to use the result of the
209/// meta-function in templates.
210template <class t_TYPE>
211using add_pointer_t = typename add_pointer<t_TYPE>::type;
212
213#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
214
215} // close namespace bsl
216
217#endif
218
219// ----------------------------------------------------------------------------
220// Copyright 2017 Bloomberg Finance L.P.
221//
222// Licensed under the Apache License, Version 2.0 (the "License");
223// you may not use this file except in compliance with the License.
224// You may obtain a copy of the License at
225//
226// http://www.apache.org/licenses/LICENSE-2.0
227//
228// Unless required by applicable law or agreed to in writing, software
229// distributed under the License is distributed on an "AS IS" BASIS,
230// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
231// See the License for the specific language governing permissions and
232// limitations under the License.
233// ----------------------------- END-OF-FILE ----------------------------------
234
235/** @} */
236/** @} */
237/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_printmethods.h:283
Definition bdlbb_blob.h:576
t_TYPE * type
Definition bslmf_addpointer.h:186
Definition bslmf_addpointer.h:169
BloombergLP::bslmf::AddPointer_Impl< t_TYPE >::type type
Definition bslmf_addpointer.h:175
Definition bslmf_addpointer.h:112
char d_dummy[99]
Definition bslmf_addpointer.h:113
Definition bslmf_addpointer.h:110
static char canFormPointer(...)
static LargeResult canFormPointer(t_TYPE *)
t_TYPE type
Definition bslmf_addpointer.h:138
Definition bslmf_addpointer.h:127
t_TYPE * type
Definition bslmf_addpointer.h:129