BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_isbitwisecopyable.h
Go to the documentation of this file.
1/// @file bslmf_isbitwisecopyable.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_isbitwisecopyable.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ISBITWISECOPYABLE
9#define INCLUDED_BSLMF_ISBITWISECOPYABLE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_isbitwisecopyable bslmf_isbitwisecopyable
15/// @brief Provide a meta-function for determining bitwise copyable types.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_isbitwisecopyable
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_isbitwisecopyable-purpose"> Purpose</a>
25/// * <a href="#bslmf_isbitwisecopyable-classes"> Classes </a>
26/// * <a href="#bslmf_isbitwisecopyable-description"> Description </a>
27/// * <a href="#bslmf_isbitwisecopyable-usage"> Usage </a>
28/// * <a href="#bslmf_isbitwisecopyable-example-1-verify-whether-types-are-trivially-copyable"> Example 1: Verify Whether Types are Trivially Copyable </a>
29///
30/// # Purpose {#bslmf_isbitwisecopyable-purpose}
31/// Provide a meta-function for determining bitwise copyable types.
32///
33/// # Classes {#bslmf_isbitwisecopyable-classes}
34///
35/// - IsBitwiseCopyable: type-traits meta-function
36/// - IsBitwiseCopyable_v: the result value of the meta-function (post-C++14)
37///
38/// @see bslmf_integralconstant, bslmf_nestedtraitdeclaration
39///
40/// # Description {#bslmf_isbitwisecopyable-description}
41/// This component defines a meta-function,
42/// `bslmf::IsBitwiseCopyable` and a template variable
43/// `bslmf::IsBitwiseCopyable_v`, that represents the result value of the
44/// `bslmf::IsBitwiseCopyable` meta-function, that may be used to query
45/// whether a type is deemed by the author to be trivially copyable, but does
46/// *NOT* have the property `bsl::is_trivially_copyable`.
47///
48/// `bslmf::IsBitwiseCopyable` has the same syntax as the
49/// `is_trivially_copyable` template from the C++11 standard [meta.unary.prop],
50/// but is used exclusively to identify types deemed by the author to be
51/// trivially copyable, but for which `bsl::is_trivially_copyable` is not `true`
52/// by default. Typically such types might have a destructor, or non-trivial
53/// creators, so that copying them via `memcpy` may be theoretically undefined
54/// behavior and cause problems on some future compilers.
55/// @code
56/// Type Category IsBitwiseCopyable by Default
57/// ------------- ----------------------------
58/// fundamental types true
59/// enumerated types true
60/// pointers to data true
61/// pointers to functions true
62/// pointers to member data true
63/// pointers to member functions true
64/// reference types false
65/// rvalue reference types false
66/// @endcode
67/// Types that are `bsl::is_trivially_copyable` are `IsBitwiseCopyable` by
68/// default -- otherwise, the only way a type acquires the `IsBitwiseCopyable`
69/// trait is by explicit specialization or by the
70/// `BSLMF_NESTED_TRAIT_DECLARATION` macro.
71///
72/// Note that the template variable `IsBitwiseCopyable_v` is defined in the
73/// C++17 standard as an inline variable. If the current compiler supports the
74/// inline variable C++17 compiler feature, `bslmf::IsBitwiseCopyable_v` is
75/// defined as an `inline constexpr bool` variable. Otherwise, if the compiler
76/// supports the variable templates C++14 compiler feature,
77/// `bslmf::IsBitwiseCopyable_v` is defined as a non-inline `constexpr bool`
78/// variable. See `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
79/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in
80/// bsls_compilerfeatures component for details.
81///
82/// ## Usage {#bslmf_isbitwisecopyable-usage}
83///
84///
85/// In this section we show intended use of this component.
86///
87/// ### Example 1: Verify Whether Types are Trivially Copyable {#bslmf_isbitwisecopyable-example-1-verify-whether-types-are-trivially-copyable}
88///
89///
90/// Suppose that we want to assert whether a type is trivially copyable, and/or
91/// bitwise copyable.
92///
93/// First, we define a set of types to evaluate:
94/// @code
95/// struct MyTriviallyCopyableType {
96/// // TRAITS
97/// BSLMF_NESTED_TRAIT_DECLARATION(MyTriviallyCopyableType,
98/// bsl::is_trivially_copyable);
99/// };
100///
101/// struct MyNonTriviallyCopyableType {
102/// // Because this 'struct' has constructors declared, the C++11 compiler
103/// // will not automatically declare it 'std::is_trivially_copyable'. But
104/// // since it has no data, we know it can be 'memcpy'ed around so we will
105/// // give it the 'IsBitwiseCopyable' trait.
106///
107/// // TRAITS
108/// BSLMF_NESTED_TRAIT_DECLARATION(MyNonTriviallyCopyableType,
109/// bslmf::IsBitwiseCopyable);
110///
111/// // CREATORS
112/// MyNonTriviallyCopyableType() {}
113/// MyNonTriviallyCopyableType(const MyNonTriviallyCopyableType&) {}
114/// ~MyNonTriviallyCopyableType() {}
115/// // Explicitly supply creators that do nothing, to ensure that this
116/// // class has no trivial traits detected with a conforming C++11
117/// // library implementation.
118/// };
119///
120/// class MyNonBitwiseCopyableType {
121/// // This 'class' allocates memory and cannot be copied around with
122/// // 'memcpy', so it should have neither the 'is_trivially_copyable' nor
123/// // the 'IsBitwiseCopyable' traits.
124///
125/// // DATA
126/// char *d_string;
127///
128/// public:
129/// // CREATORS
130/// MyNonBitwiseCopyableType(const char *string)
131/// : d_string(::strdup(string))
132/// {}
133///
134/// MyNonBitwiseCopyableType(const MyNonBitwiseCopyableType& original)
135/// : d_string(::strdup(original.d_string))
136/// {}
137///
138/// ~MyNonBitwiseCopyableType()
139/// {
140/// free(d_string);
141/// }
142///
143/// bool operator==(const MyNonBitwiseCopyableType& rhs) const
144/// {
145/// return !::strcmp(d_string, rhs.d_string);
146/// }
147/// };
148/// @endcode
149/// Then, the following 5 types are automatically interpreted by
150/// `bsl::is_trivially_copyable` to be trivially copyable without our having to
151/// declare them as such, and therefore, as `IsBitwiseCopyable`.
152/// @code
153/// typedef int MyFundamentalType;
154/// // fundamental type
155///
156/// typedef int *DataPtrTestType;
157/// // data pointer
158///
159/// typedef void (*FunctionPtrTestType)();
160/// // function ptr
161///
162/// typedef int MyNonBitwiseCopyableType::*DataMemberPtrTestType;
163/// // non-static data member ptr
164///
165/// typedef int (MyNonBitwiseCopyableType::*MethodPtrTestType)();
166/// // non-static function member ptr
167/// @endcode
168/// The following 2 types are neither trivially nor bitwise copyable:
169/// @code
170/// typedef int& MyFundamentalTypeRef;
171/// // reference (not bitwise copyable)
172///
173/// #if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
174/// typedef int&& MyFundamentalTypeRvalueRef;
175/// // rvalue reference (not bitwise copyable)
176/// #endif
177/// @endcode
178/// Now, we verify whether each type is trivially copyable using
179/// `bsl::is_trivially_copyable`:
180/// @code
181/// assert( bsl::is_trivially_copyable<MyTriviallyCopyableType>::value);
182/// assert(!bsl::is_trivially_copyable<MyNonTriviallyCopyableType>::value);
183/// assert(!bsl::is_trivially_copyable<MyNonBitwiseCopyableType>::value);
184///
185/// assert( bsl::is_trivially_copyable<MyFundamentalType>::value);
186/// assert( bsl::is_trivially_copyable<DataPtrTestType>::value);
187/// assert( bsl::is_trivially_copyable<FunctionPtrTestType>::value);
188/// assert( bsl::is_trivially_copyable<DataMemberPtrTestType>::value);
189/// assert( bsl::is_trivially_copyable<MethodPtrTestType>::value);
190/// assert(!bsl::is_trivially_copyable<MyFundamentalTypeRef>::value);
191/// #if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
192/// assert(!bsl::is_trivially_copyable<MyFundamentalTypeRvalueRef>::value);
193/// #endif
194/// @endcode
195/// Now, we verify whether each type is bitwise copyable using
196/// `bslmf::IsBitwiseCopyable`:
197/// @code
198/// assert( bslmf::IsBitwiseCopyable<MyTriviallyCopyableType>::value);
199/// assert( bslmf::IsBitwiseCopyable<MyNonTriviallyCopyableType>::value);
200/// assert(!bslmf::IsBitwiseCopyable<MyNonBitwiseCopyableType>::value);
201///
202/// assert( bslmf::IsBitwiseCopyable<MyFundamentalType>::value);
203/// assert( bslmf::IsBitwiseCopyable<DataPtrTestType>::value);
204/// assert( bslmf::IsBitwiseCopyable<FunctionPtrTestType>::value);
205/// assert( bslmf::IsBitwiseCopyable<DataMemberPtrTestType>::value);
206/// assert( bslmf::IsBitwiseCopyable<MethodPtrTestType>::value);
207/// assert(!bslmf::IsBitwiseCopyable<MyFundamentalTypeRef>::value);
208/// #if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
209/// assert(!bslmf::IsBitwiseCopyable<MyFundamentalTypeRvalueRef>::value);
210/// #endif
211/// @endcode
212/// Finally, note that if the current compiler supports the variable templates
213/// C++14 feature, then we can re-write the two snippets of code above as
214/// follows:
215/// @code
216/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
217/// // trivially copyable:
218///
219/// assert( bsl::is_trivially_copyable_v<MyTriviallyCopyableType>);
220/// assert(!bsl::is_trivially_copyable_v<MyNonTriviallyCopyableType>);
221/// assert(!bsl::is_trivially_copyable_v<MyNonBitwiseCopyableType>);
222///
223/// assert( bsl::is_trivially_copyable_v<MyFundamentalType>);
224/// assert( bsl::is_trivially_copyable_v<DataPtrTestType>);
225/// assert( bsl::is_trivially_copyable_v<FunctionPtrTestType>);
226/// assert( bsl::is_trivially_copyable_v<DataMemberPtrTestType>);
227/// assert( bsl::is_trivially_copyable_v<MethodPtrTestType>);
228/// assert(!bsl::is_trivially_copyable_v<MyFundamentalTypeRef>);
229/// assert(!bsl::is_trivially_copyable_v<MyFundamentalTypeRvalueRef>);
230///
231/// // bitwise copyable:
232///
233/// assert( bslmf::IsBitwiseCopyable_v<MyTriviallyCopyableType>);
234/// assert( bslmf::IsBitwiseCopyable_v<MyNonTriviallyCopyableType>);
235/// assert(!bslmf::IsBitwiseCopyable_v<MyNonBitwiseCopyableType>);
236///
237/// assert( bslmf::IsBitwiseCopyable_v<MyFundamentalType>);
238/// assert( bslmf::IsBitwiseCopyable_v<DataPtrTestType>);
239/// assert( bslmf::IsBitwiseCopyable_v<FunctionPtrTestType>);
240/// assert( bslmf::IsBitwiseCopyable_v<DataMemberPtrTestType>);
241/// assert( bslmf::IsBitwiseCopyable_v<MethodPtrTestType>);
242/// assert(!bslmf::IsBitwiseCopyable_v<MyFundamentalTypeRef>);
243/// assert(!bslmf::IsBitwiseCopyable_v<MyFundamentalTypeRvalueRef>);
244/// #endif
245/// @endcode
246/// @}
247/** @} */
248/** @} */
249
250/** @addtogroup bsl
251 * @{
252 */
253/** @addtogroup bslmf
254 * @{
255 */
256/** @addtogroup bslmf_isbitwisecopyable
257 * @{
258 */
259
260#include <bslscm_version.h>
261
265
267#include <bsls_keyword.h>
268#include <bsls_platform.h>
269
270#include <stddef.h>
271
272#ifdef BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
273# include <type_traits>
274#endif // BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
275
276
277namespace bslmf {
278
279 // ========================
280 // struct IsBitwiseCopyable
281 // ========================
282
283template <class t_TYPE>
284struct IsBitwiseCopyable;
285
286#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
287/// This template variable represents the result value of the
288/// `bslmf::IsBitwiseCopyable` meta-function.
289template <class t_TYPE>
290BSLS_KEYWORD_INLINE_VARIABLE constexpr bool IsBitwiseCopyable_v =
292#endif
293
294template <class t_TYPE>
296 bool,
297 DetectNestedTrait<t_TYPE, IsBitwiseCopyable>::value
298 || bsl::is_trivially_copyable<t_TYPE>::value>::type {
299};
300
301/// This partial specialization optimizes away a number of nested template
302/// instantiations to ensure that reference types are never bitwise
303/// copyable.
304template <class t_TYPE>
306};
307
308#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
309/// This partial specialization optimizes away a number of nested template
310/// instantiations to ensure that rvalue reference types are never bitwise
311/// copyable.
312template <class t_TYPE>
313struct IsBitwiseCopyable<t_TYPE&&> : bsl::false_type {
314};
315#endif
316
317template <class t_TYPE>
318struct IsBitwiseCopyable<const t_TYPE> : IsBitwiseCopyable<t_TYPE> {};
319
320template <class t_TYPE>
321struct IsBitwiseCopyable<volatile t_TYPE> : IsBitwiseCopyable<t_TYPE> {};
322
323template <class t_TYPE>
324struct IsBitwiseCopyable<const volatile t_TYPE> : IsBitwiseCopyable<t_TYPE> {};
325
326template <class t_TYPE, int t_LEN>
327struct IsBitwiseCopyable<t_TYPE[t_LEN]> : IsBitwiseCopyable<t_TYPE> {};
328
329template <class t_TYPE, int t_LEN>
330struct IsBitwiseCopyable<const t_TYPE[t_LEN]> : IsBitwiseCopyable<t_TYPE> {};
331
332template <class t_TYPE, int t_LEN>
333struct IsBitwiseCopyable<volatile t_TYPE[t_LEN]> :
334 IsBitwiseCopyable<t_TYPE> {};
335
336template <class t_TYPE, int t_LEN>
337struct IsBitwiseCopyable<const volatile t_TYPE[t_LEN]> :
338 IsBitwiseCopyable<t_TYPE> {};
339
340template <class t_TYPE>
341struct IsBitwiseCopyable<t_TYPE[]> : IsBitwiseCopyable<t_TYPE>::type {};
342template <class t_TYPE>
343struct IsBitwiseCopyable<const t_TYPE[]> : IsBitwiseCopyable<t_TYPE>::type {};
344
345template <class t_TYPE>
346struct IsBitwiseCopyable<volatile t_TYPE[]> :
347 IsBitwiseCopyable<t_TYPE>::type {};
348/// These partial specializations ensures that array-of-unknown-bound types
349/// have the same result as their element type.
350template <class t_TYPE>
351struct IsBitwiseCopyable<const volatile t_TYPE[]>
352 : IsBitwiseCopyable<t_TYPE>::type {};
353
354} // close namespace bslmf
355
356
357#endif // ! defined(INCLUDED_BSLMF_ISBITWISECOPYABLE)
358
359// ----------------------------------------------------------------------------
360// Copyright 2023 Bloomberg Finance L.P.
361//
362// Licensed under the Apache License, Version 2.0 (the "License");
363// you may not use this file except in compliance with the License.
364// You may obtain a copy of the License at
365//
366// http://www.apache.org/licenses/LICENSE-2.0
367//
368// Unless required by applicable law or agreed to in writing, software
369// distributed under the License is distributed on an "AS IS" BASIS,
370// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
371// See the License for the specific language governing permissions and
372// limitations under the License.
373// ----------------------------- END-OF-FILE ----------------------------------
374
375/** @} */
376/** @} */
377/** @} */
static const t_TYPE value
Definition bslmf_integralconstant.h:258
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_INLINE_VARIABLE
Definition bsls_keyword.h:623
Definition bdlbb_blob.h:576
Definition bslmf_integralconstant.h:244
Definition bslmf_isbitwisecopyable.h:298