BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_iscopyconstructible.h
Go to the documentation of this file.
1/// @file bslmf_iscopyconstructible.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_iscopyconstructible.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ISCOPYCONSTRUCTIBLE
9#define INCLUDED_BSLMF_ISCOPYCONSTRUCTIBLE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_iscopyconstructible bslmf_iscopyconstructible
15/// @brief Provide a meta-function to report if a type is copy constructible.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_iscopyconstructible
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_iscopyconstructible-purpose"> Purpose</a>
25/// * <a href="#bslmf_iscopyconstructible-classes"> Classes </a>
26/// * <a href="#bslmf_iscopyconstructible-description"> Description </a>
27///
28/// # Purpose {#bslmf_iscopyconstructible-purpose}
29/// Provide a meta-function to report if a type is copy constructible.
30///
31/// # Classes {#bslmf_iscopyconstructible-classes}
32///
33/// - bsl::is_copy_constructible: type-traits meta-function
34/// - bsl::is_copy_constructible_v: the result value of the meta-function
35///
36/// @see bslmf_integralconstant, bslmf_nestedtraitdeclaration
37///
38/// # Description {#bslmf_iscopyconstructible-description}
39/// This component defines a meta-function,
40/// `bsl::is_copy_constructible` and a template variable
41/// `bsl::is_copy_constructible_v`, that represents the result value of the
42/// `bsl::is_copy_constructible` meta-function, that may be used to query
43/// whether a type is copy constructible.
44///
45/// `bsl::is_copy_constructible` has the same syntax as the
46/// `is_copy_constructible` template from the C++11 standard [meta.unary.prop].
47/// Indeed, in C++11 compilation environments, `bsl::is_copy_constructible`
48/// simply forwards to the native implementation, which can determine the
49/// correct value for all types without requiring specialization; in C++03
50/// environments, `bsl::is_copy_construcible` provides welcome backward
51/// compatibility but returns `true` for all user-defined types and requires
52/// explicit specialization for types that are not copy constructible (e.g.,
53/// move-only types).
54///
55/// Note that the `bsl::is_copy_constructible` trait cannot be declared as a
56/// nested trait because the default value of the trait is `true` and nested
57/// traits work properly only for traits that have default value `false`. In
58/// order to indicate that a certain type `T` is not copy constructible, the
59/// following idiom should be used:
60/// @code
61/// namespace bsl {
62/// template <>
63/// struct is_copy_constructible<T> : false_type { };
64/// }
65/// @endcode
66/// Also note that the template variable `is_copy_constructible_v` is defined in
67/// the C++17 standard as an inline variable. If the current compiler supports
68/// the inline variable C++17 compiler feature, `bsl::is_copy_constructible_v`
69/// is defined as an `inline constexpr bool` variable. Otherwise, if the
70/// compiler supports the variable templates C++14 compiler feature,
71/// `bsl::is_copy_constructible_v` is defined as a non-inline `constexpr bool`
72/// variable. See `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
73/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in
74/// bsls_compilerfeatures component for details.
75/// @}
76/** @} */
77/** @} */
78
79/** @addtogroup bsl
80 * @{
81 */
82/** @addtogroup bslmf
83 * @{
84 */
85/** @addtogroup bslmf_iscopyconstructible
86 * @{
87 */
88
89#include <bslscm_version.h>
90
93#include <bslmf_isarray.h>
95#include <bslmf_isfunction.h>
96#include <bslmf_isreference.h>
97#include <bslmf_isvolatile.h>
98
100#include <bsls_keyword.h>
101
102#if defined(BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER)
103# include <type_traits>
104#endif // BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
105
106#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
107#include <bsls_nativestd.h>
108#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
109
110#if defined(BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER)
111# define BSLS_ISCOPYCONSTRUCTIBLE_USE_NATIVE_TRAIT 1
112#endif
113
114namespace bsl {
115
116 // ============================
117 // struct is_copy_constructible
118 // ============================
119
120/// This `struct` template implements a meta-function to determine whether
121/// the (template parameter) `t_TYPE` is copy constructible. This `struct`
122/// derives from `bsl::true_type` if the `t_TYPE` is copy constructible, and
123/// from `bsl::false_type` otherwise. This meta-function has the same
124/// syntax as the `is_copy_constructible` meta-function defined in the C++11
125/// standard [meta.unary.prop]; on C++03 platforms, however, this
126/// meta-function defaults to `true_type` for all types that are not
127/// explicitly declared to have the `bslmf::IsNonCopyable` trait using the
128/// `BSLMF_NESTED_TRAIT_DECLARATION` macro. To mark a type as non-copyable,
129/// `bslmf::IsNonCopyable` must be specialized (for that type) to inherit
130/// from `bsl::true_type`.
131template <class t_TYPE>
132struct is_copy_constructible;
133
134} // close namespace bsl
135
136// ============================================================================
137// CLASS TEMPLATE DEFINITIONS
138// ============================================================================
139
140#if defined(BSLS_ISCOPYCONSTRUCTIBLE_USE_NATIVE_TRAIT)
141namespace bsl {
142
143 // ====================================
144 // struct is_copy_constructible (C++11)
145 // ====================================
146
147///Implementation Notes
148///--------------------
149// In C++20 certain array types which decay to their element type, such as
150// 'const void*' and 'bool', report as copy constructible through
151// 'std::is_copy_constructible'. In fact, initialization that attempts such
152// copy constructions does not result in a copy of the original array but
153// instead initializes only the first element of the new array -- and that is
154// set to the address of the original array, not the original's first element.
155//
156// This behavior has been observed in 'gcc-11' targeting C++20 and should occur
157// with any compiler correctly supporting C++20 aggregate initialization with
158// parenthesis (identified by the feature test macro
159// '__cpp_aggregate_paren_init').
160//
161// An LWG issue (https://wg21.link/lwg####) has been filed to correct this
162// behavior and continue to return 'false' for all array types.
163//
164// The implementation below preemptively implements the expected resolution of
165// that issue on all platforms.
166
167/// This specialization largely defers to the native trait on supported
168/// C++11 compilers. See {Implementation Notes} above.
169template <class t_TYPE>
170struct is_copy_constructible
172 (bsl::is_array<t_TYPE>::value
173 ? false
174 : std::is_copy_constructible<t_TYPE>::value)> {
175};
176
177#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
178/// This template variable represents the result value of the
179/// `bsl::is_copy_constructible` meta-function.
180template <class t_TYPE>
181BSLS_KEYWORD_INLINE_VARIABLE constexpr bool is_copy_constructible_v =
182 is_copy_constructible<t_TYPE>::value;
183#endif
184
185} // close namespace bsl
186
187#else
188
189
190namespace bslmf {
191
192 // ==============================
193 // struct IsCopyConstructible_Imp
194 // ==============================
195
196/// This `struct` template implements a meta-function to determine whether
197/// the (non-cv-qualified) (template parameter) `t_TYPE` has a copy
198/// constructor.
199template <class t_TYPE>
202 bslmf::IsBitwiseCopyable<t_TYPE>::value ||
203 bsl::is_reference<t_TYPE>::value ||
204 !(bsl::is_volatile<t_TYPE>::value ||
205 bsl::is_function<t_TYPE>::value)> {
206};
207
208/// This explicit specialization reports that `void` does not have a copy
209/// constructor, despite being a fundamental type.
210template <>
212{
213};
214
215/// This explicit specialization reports that `volatile void` does not have
216/// a copy constructor, despite being a fundamental type.
217template <>
219{
220};
221
222/// This explicit specialization reports that volatile pointer objects have
223/// a copy constructor, just like a fundamental type.
224template <class t_TYPE>
225struct IsCopyConstructible_Imp<t_TYPE *volatile> : bsl::true_type {
226};
227
228} // close package namespace
229
230
231namespace bsl {
232
233 // ====================================
234 // struct is_copy_constructible (C++03)
235 // ====================================
236
237/// The primary template for this traits handles only non-`const`-qualified
238/// types; partial specializations will handle some interesting cases,
239/// including the remaining `const`-qualified types.
240template <class t_TYPE>
242: BloombergLP::bslmf::IsCopyConstructible_Imp<t_TYPE>::type {
243};
244
245/// This partial specialization ensures that const-qualified types have the
246/// same result as their element type.
247template <class t_TYPE>
248struct is_copy_constructible<const t_TYPE>
249: is_copy_constructible<t_TYPE>::type {
250};
251
252/// This partial specialization ensures that array types have the result
253/// `false`.
254template <class t_TYPE, size_t t_LEN>
255struct is_copy_constructible<t_TYPE[t_LEN]> : false_type {
256};
257
258/// This partial specialization ensures that const-qualified array types
259/// have result `false`.
260template <class t_TYPE, size_t t_LEN>
261struct is_copy_constructible<const t_TYPE[t_LEN]> : false_type {
262};
263
264/// This partial specialization ensures that volatile-qualified array types
265/// have the result `false`.
266template <class t_TYPE, size_t t_LEN>
267struct is_copy_constructible<volatile t_TYPE[t_LEN]> : false_type {
268};
269
270/// This partial specialization ensures that const-volatile-qualified array
271/// types have the result false.
272template <class t_TYPE, size_t t_LEN>
273struct is_copy_constructible<const volatile t_TYPE[t_LEN]> : false_type {
274};
275
276#if !defined(BSLS_PLATFORM_CMP_IBM)
277// Last checked with the xlC 12.1 compiler. The IBM xlC compiler has problems
278// correctly handling arrays of unknown bound as template parameters.
279
280/// This partial specialization ensures that array-of-unknown-bound types
281/// have the result `false`.
282template <class t_TYPE>
284};
285
286/// This partial specialization ensures that const-qualified
287/// array-of-unknown-bound types have the result `false`.
288template <class t_TYPE>
289struct is_copy_constructible<const t_TYPE[]> : false_type {
290};
291
292/// This partial specialization ensures that volatile-qualified
293/// array-of-unknown-bound types have the result `false`.
294template <class t_TYPE>
295struct is_copy_constructible<volatile t_TYPE[]> : false_type {
296};
297
298/// This partial specialization ensures that const-volatile-qualified
299/// array-of-unknown-bound types have the result `false`.
300template <class t_TYPE>
301struct is_copy_constructible<const volatile t_TYPE[]> : false_type {
302};
303
304#endif // defined(BSLS_PLATFORM_CMP_IBM)
305
306} // close namespace bsl
307
308#endif // defined(BSLS_ISCOPYCONSTRUCTIBLE_USE_NATIVE_TRAIT)
309
310#endif
311
312// ----------------------------------------------------------------------------
313// Copyright 2016 Bloomberg Finance L.P.
314//
315// Licensed under the Apache License, Version 2.0 (the "License");
316// you may not use this file except in compliance with the License.
317// You may obtain a copy of the License at
318//
319// http://www.apache.org/licenses/LICENSE-2.0
320//
321// Unless required by applicable law or agreed to in writing, software
322// distributed under the License is distributed on an "AS IS" BASIS,
323// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
324// See the License for the specific language governing permissions and
325// limitations under the License.
326// ----------------------------- END-OF-FILE ----------------------------------
327
328/** @} */
329/** @} */
330/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_INLINE_VARIABLE
Definition bsls_keyword.h:623
Definition bdlb_printmethods.h:283
Definition bdlbb_blob.h:576
Definition bslmf_integralconstant.h:244
Definition bslmf_iscopyconstructible.h:242
Definition bslmf_iscopyconstructible.h:205