BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_isnothrowmoveconstructible.h
Go to the documentation of this file.
1/// @file bslmf_isnothrowmoveconstructible.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_isnothrowmoveconstructible.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ISNOTHROWMOVECONSTRUCTIBLE
9#define INCLUDED_BSLMF_ISNOTHROWMOVECONSTRUCTIBLE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_isnothrowmoveconstructible bslmf_isnothrowmoveconstructible
15/// @brief Provide metafunction to identify no-throw move-constructible types.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_isnothrowmoveconstructible
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_isnothrowmoveconstructible-purpose"> Purpose</a>
25/// * <a href="#bslmf_isnothrowmoveconstructible-classes"> Classes </a>
26/// * <a href="#bslmf_isnothrowmoveconstructible-description"> Description </a>
27///
28/// # Purpose {#bslmf_isnothrowmoveconstructible-purpose}
29/// Provide metafunction to identify no-throw move-constructible types.
30///
31/// # Classes {#bslmf_isnothrowmoveconstructible-classes}
32///
33/// - bsl::is_nothrow_move_constructible: type-traits metafunction
34/// - bsl::is_nothrow_move_constructible_v: the metafunction's result value
35///
36/// @see bslmf_integralconstant, bslmf_nestedtraitdeclaration,
37/// bslmf_movableref
38///
39/// # Description {#bslmf_isnothrowmoveconstructible-description}
40/// This component defines a metafunction,
41/// `bsl::is_nothrow_move_constructible`, and a variable template
42/// `bsl::is_nothrow_move_constructible_v` that represents the result value of
43/// the `bsl::is_nothrow_move_constructible` metafunction, which may be used to
44/// query whether a type has a constructor that can be called with a single
45/// rvalue that is known to not throw exceptions. Note that a C++11 compiler
46/// will automatically infer this trait for class types with a move constructor
47/// that is marked as `noexcept`.
48///
49/// `bsl::is_nothrow_move_constructible` meets the requirements of the
50/// `is_nothrow_move_constructible` template defined in the C++11 standard.
51///
52/// Note that the template variable `is_nothrow_move_constructible_v` is defined
53/// in the C++17 standard as an inline variable. If the current compiler
54/// supports the inline variable C++17 compiler feature,
55/// `bsl::is_nothrow_move_constructible_v` is defined as an 'inline constexpr
56/// bool' variable. Otherwise, if the compiler supports the variable templates
57/// C++14 compiler feature, `bsl::is_nothrow_move_constructible_v` is defined as
58/// a non-inline `constexpr bool` variable. See
59/// `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
60/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in the
61/// bsls_compilerfeatures component for details.
62/// @}
63/** @} */
64/** @} */
65
66/** @addtogroup bsl
67 * @{
68 */
69/** @addtogroup bslmf
70 * @{
71 */
72/** @addtogroup bslmf_isnothrowmoveconstructible
73 * @{
74 */
75
76#include <bslscm_version.h>
77
78#include <bslmf_assert.h>
81#include <bslmf_isarray.h>
83#include <bslmf_isconst.h>
84#include <bslmf_isvolatile.h>
85#include <bslmf_voidtype.h>
86
88#include <bsls_keyword.h>
89#include <bsls_platform.h>
90
91#include <stddef.h>
92
93#ifdef BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
94# include <type_traits>
95#endif // BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
96
97#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
98#include <bsls_nativestd.h>
99#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
100
101namespace bsl {
102
103template <class t_TYPE>
104struct is_nothrow_move_constructible;
105
106} // close namespace bsl
107
108///Implementation Notes
109///--------------------
110// In C++20 certain array types which decay to their element type, such as
111// 'const void*' and 'bool', report as copy constructible through
112// 'std::is_nothrow_move_constructible'. In fact, initialization that attempts
113// such move constructions does not result in a copy of the orginal array but
114// instead initializes only the first element of the new array -- and that is
115// set to the address of the original array, not the original's first element.
116//
117// This behavior has been observed in 'gcc-11' targeting C++20 and should occur
118// with any compiler correctly supporting C++20 aggregate initialization with
119// parenthesis (identified by the feature test macro
120// '__cpp_aggregate_paren_init').
121//
122// An LWG issue (https://wg21.link/lwg####) has been filed to correct this
123// behavior and continue to return 'false' for all array types.
124//
125// The implementation below preemptively implements the expected resolution of
126// that issue on all platforms.
127
128#define STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE(t_TYPE) \
129 (bsl::is_array<t_TYPE>::value \
130 ? false \
131 : ::std::is_nothrow_move_constructible<t_TYPE>::value)
132
133
134namespace bslmf {
135
136 // =====================================
137 // struct IsNothrowMoveConstructible_Imp
138 // =====================================
139
140#if defined(BSLS_PLATFORM_CMP_SUN) && \
141 (BSLS_PLATFORM_CMP_VERSION == 0x5150) && \
142 (BSLS_COMPILERFEATURES_CPLUSPLUS == 199711L)
143// Solaris 12.6 compiler in C++03 mode treats as ambiguous partial
144// specializations with `t_TYPE` and with cv-qualified `t_TYPE`.
145#define BSLMF_ISNOTHROWMOVECONSTRUCTIBLE_VOIDTYPE(t_TYPE) \
146 typename bsl::enable_if<!bsl::is_const<t_TYPE>::value && \
147 !bsl::is_volatile<t_TYPE>::value, \
148 BSLMF_VOIDTYPE(int t_TYPE::*)>::type
149#else
150#define BSLMF_ISNOTHROWMOVECONSTRUCTIBLE_VOIDTYPE(t_TYPE) \
151 BSLMF_VOIDTYPE(int t_TYPE::*)
152#endif
153
154#if defined(BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER)
155/// This `struct` template implements a metafunction to determine whether
156/// the (non-cv-qualified) (template parameter) `t_TYPE` has a no-throw move
157/// constructor. Note that the partial specializations below will provide
158/// the determination for class types.
159template <class t_TYPE, class = void>
160struct IsNothrowMoveConstructible_Impl
162 STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE(t_TYPE)> {
163};
164
165/// This `struct` template implements a metafunction to determine whether
166/// the (non-cv-qualified) (template parameter) `t_TYPE` has a no-throw move
167/// constructor. To maintain consistency between the C++03 and C++11
168/// implementations of this trait, types that use the BDE trait association
169/// techniques are also detected as no-throw move constructible, even if the
170/// `noexcept` operator would draw a different conclusion.
171template <class t_TYPE>
172struct IsNothrowMoveConstructible_Impl<
173 t_TYPE,
176 bool,
177 STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE(t_TYPE) ||
178 bslmf::IsBitwiseCopyable<t_TYPE>::value ||
179 DetectNestedTrait<t_TYPE,
180 bsl::is_nothrow_move_constructible>::value> {
181
182 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
183};
184
185template <class t_TYPE>
186struct IsNothrowMoveConstructible_Impl<
187 const t_TYPE,
190 bool,
191 STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE(const t_TYPE) ||
192 bslmf::IsBitwiseCopyable<t_TYPE>::value> {
193 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
194};
195
196template <class t_TYPE>
197struct IsNothrowMoveConstructible_Impl<
198 volatile t_TYPE,
201 bool,
202 STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE(volatile t_TYPE)> {
203 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
204};
205
206template <class t_TYPE>
207struct IsNothrowMoveConstructible_Impl<
208 const volatile t_TYPE,
211 bool,
212 STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE(const volatile t_TYPE)> {
213 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
214};
215 // Partial specializations to avoid detecting cv-qualified class types with
216 // nested trait declarations as having no-throw constructors that take
217 // cv-qualified rvalues unless those constructors are actually detected as
218 // non-throwing. Note that volatile-qualified scalar types will detect as
219 // no-throw move constructible through the primary template. In addition,
220 // we flag types that specialize 'bslmf::IsBitwiseCopyable' as no-throw
221 // move constructible to maintain consistency between the C++03 and C++11
222 // implementations of this trait.
223
224#undef STD_IS_NOTHROW_MOVE_CONSTRUCTIBLE_VALUE
225#else
226/// This `struct` template implements a metafunction to determine whether
227/// the (non-cv-qualified) (template parameter) `t_TYPE` has a no-throw move
228/// constructor. For C++03, the set of types known to be no-throw move
229/// constructible are all trivial types. Note that the partial
230/// specializations below will provide the determination for class types,
231/// and this primary template is equivalent to querying whether `t_TYPE` is
232/// a scalar type.
233template <class t_TYPE, class = void>
237
238/// This `struct` template implements a metafunction to determine whether
239/// the (non-cv-qualified) (template parameter) `t_TYPE` has a no-throw move
240/// constructor. To maintain consistency between the C++03 and C++11
241/// implementations of this trait, types that use the BDE trait association
242/// techniques are also detected as no-throw move constructible, even if the
243/// `noexcept` operator would draw a different conclusion.
244template <class t_TYPE>
246 t_TYPE,
249 bool,
250 bslmf::IsBitwiseCopyable<t_TYPE>::value ||
251 DetectNestedTrait<t_TYPE,
252 bsl::is_nothrow_move_constructible>::value> {
253
254 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
255};
256
257template <class t_TYPE>
259 const t_TYPE,
261: bslmf::IsBitwiseCopyable<t_TYPE>::type {
262 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
263};
264
265template <class t_TYPE>
267 volatile t_TYPE,
269 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
270};
271
272template <class t_TYPE>
274 const volatile t_TYPE,
276 enum { k_CHECK_COMPLETE = sizeof(t_TYPE) }; // Diagnose incomplete types
277};
278 // Partial specializations to avoid detecting cv-qualified class types with
279 // nested trait declarations as having no-throw constructors that take
280 // cv-qualified rvalues unless those constructors are actually detected.
281 // Note that volatile-qualified scalar types will detect as no-throw move
282 // constructible through the primary template. As a practical matter, we
283 // assume that types flagged as trivially copyable have a no-throw copy
284 // constructor, and so are no-throw move constructible too.
285
286#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
287template <class t_TYPE>
288struct IsNothrowMoveConstructible_Impl<t_TYPE&&> : bsl::true_type {
289};
290#endif
291template <class t_TYPE>
294 // Partial specializations to indicate that reference binding from an
295 // identical reference can never throw.
296
297template <class t_TYPE>
300template <class t_TYPE>
302};
303template <class t_TYPE>
305};
306template <class t_TYPE>
307struct IsNothrowMoveConstructible_Impl<const volatile t_TYPE[]>
309};
310template <class t_TYPE, size_t t_LEN>
312};
313template <class t_TYPE, size_t t_LEN>
315};
316template <class t_TYPE, size_t t_LEN>
317struct IsNothrowMoveConstructible_Impl<volatile t_TYPE[t_LEN]>
319};
320template <class t_TYPE, size_t t_LEN>
321struct IsNothrowMoveConstructible_Impl<const volatile t_TYPE[t_LEN]>
323};
324 // These partial specializations ensure that array types are not detected
325 // as move constructible. Note that while array data members of classes
326 // will correctly move each element through move-construction (when
327 // initializing the owning class), the standard defines the type trait in
328 // terms of a variable declaration, where the move construction syntax is
329 // invalid.
330
331#endif
332} // close package namespace
333
334
335namespace bsl {
336
337 // ====================================
338 // struct is_nothrow_move_constructible
339 // ====================================
340
341/// This `struct` template implements a metafunction to determine whether
342/// the (template parameter) `t_TYPE` has a no-throw move constructor. This
343/// `struct` derives from `bsl::true_type` if the `t_TYPE` has a no-throw
344/// move constructor, and from `bsl::false_type` otherwise. This
345/// metafunction has the same syntax as the `is_nothrow_move_constructible`
346/// metafunction defined in the C++11 standard [meta.unary.prop]; on C++03
347/// platforms, however, this metafunction can automatically determine the
348/// value for trivially copyable types (including scalar types), for
349/// reference types, and for class types associating with the
350/// `bsl::is_nothrow_move_constructible` trait using the
351/// `BSLMF_NESTED_TRAIT_DECLARATION` macro. To support other no-throw move
352/// constructible types, this template should be specialized to inherit from
353/// `bsl::true_type`. Note that cv-qualified user defined types are rarely
354/// no-throw move constructible unless they are also trivially copyable, so
355/// there are no cv-qualified partial specializations of this trait.
356template <class t_TYPE>
358: BloombergLP::bslmf::IsNothrowMoveConstructible_Impl<t_TYPE>::type {
359};
360
361#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
362/// This template variable represents the result value of the
363/// `bsl::is_nothrow_move_constructible` metafunction.
364template <class t_TYPE>
365BSLS_KEYWORD_INLINE_VARIABLE constexpr bool is_nothrow_move_constructible_v =
367#endif
368
369} // close namespace bsl
370
371#endif
372
373// ----------------------------------------------------------------------------
374// Copyright 2019 Bloomberg Finance L.P.
375//
376// Licensed under the Apache License, Version 2.0 (the "License");
377// you may not use this file except in compliance with the License.
378// You may obtain a copy of the License at
379//
380// http://www.apache.org/licenses/LICENSE-2.0
381//
382// Unless required by applicable law or agreed to in writing, software
383// distributed under the License is distributed on an "AS IS" BASIS,
384// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
385// See the License for the specific language governing permissions and
386// limitations under the License.
387// ----------------------------- END-OF-FILE ----------------------------------
388
389/** @} */
390/** @} */
391/** @} */
#define BSLMF_ISNOTHROWMOVECONSTRUCTIBLE_VOIDTYPE(t_TYPE)
Definition bslmf_isnothrowmoveconstructible.h:150
#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_isnothrowmoveconstructible.h:358
Definition bslmf_isbitwisecopyable.h:298
Definition bslmf_isnothrowmoveconstructible.h:235