BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_function_isreferencecompatible.h
Go to the documentation of this file.
1/// @file bslstl_function_isreferencecompatible.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_function_isreferencecompatible.h -*-C++-*-
8
9#ifndef INCLUDED_BSLSTL_FUNCTION_ISREFERENCECOMPATIBLE
10#define INCLUDED_BSLSTL_FUNCTION_ISREFERENCECOMPATIBLE
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslstl_function_isreferencecompatible bslstl_function_isreferencecompatible
16/// @brief Provide a metafunction for substitutability of type references.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslstl
20/// @{
21/// @addtogroup bslstl_function_isreferencecompatible
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslstl_function_isreferencecompatible-purpose"> Purpose</a>
26/// * <a href="#bslstl_function_isreferencecompatible-classes"> Classes </a>
27/// * <a href="#bslstl_function_isreferencecompatible-description"> Description </a>
28///
29/// # Purpose {#bslstl_function_isreferencecompatible-purpose}
30/// Provide a metafunction for substitutability of type references.
31///
32/// # Classes {#bslstl_function_isreferencecompatible-classes}
33///
34/// - bslstl::Function_IsReferenceCompatible : Boolean metafunction
35///
36/// @see bslstl_function, bdef_function
37///
38/// # Description {#bslstl_function_isreferencecompatible-description}
39/// This private, subordinate component to @ref bslstl_function
40/// provides a Boolean metafunction, `bslstl::Function_IsReferenceCompatible`,
41/// which allows generic code to determine whether a reference to the first
42/// template parameter type (`FROM_TYPE`) can be substituted for a reference to
43/// the second template parameter type (`TO_TYPE`) with no loss of information.
44/// By default, this metafunction yields `true_type` if `FROM_TYPE` is the same
45/// as `TO_TYPE`; else it yields `false_type`. However, this template can be
46/// specialized to yield `true_type` for other parameters that have compatible
47/// references. In practice, this metafunction is used to detect types, such as
48/// `bdef_Function`, that convert to `bsl::function` and wrap it with no
49/// additional data members; `bslstl::Function_IsReferenceCompatible` would be
50/// specialized to yield `true_type` for such wrapper types. This metafunction
51/// is used within an `enable_if` to prevent types that are reference compatible
52/// with `bsl::function` from matching template parameters in `function`
53/// constructors and assignment operators, preferring, instead, the non-template
54/// overloads for copy and move construction and assignment. Note that
55/// reference qualifiers on `FROM_TYPE` and `TO_TYPE` will cause instantiation
56/// to fail; the caller should strip reference qualifiers on `FROM_TYPE` and
57/// `TO_TYPE` before checking for reference compatibility. This component
58/// exists solely to provide a transition from `bdef_Function` to
59/// `bsl::function` so that the former can eventually be deprecated and removed.
60/// This component should therefore also be considered deprecated and will be
61/// removed when `bdef_Function` is removed.
62///
63/// @}
64/** @} */
65/** @} */
66
67/** @addtogroup bsl
68 * @{
69 */
70/** @addtogroup bslstl
71 * @{
72 */
73/** @addtogroup bslstl_function_isreferencecompatible
74 * @{
75 */
76
77#include <bslscm_version.h>
78
79#include <bslmf_assert.h>
80#include <bslmf_conditional.h>
81#include <bslmf_issame.h>
82#include <bslmf_movableref.h>
83
85
86
87namespace bslstl {
88
89 // =============================================
90 // class template Function_IsReferenceCompatible
91 // =============================================
92
93/// This metafunction is derived from `true_type` if a reference to the
94/// specified `FROM_TYPE` parameter type can be substituted for a reference
95/// to the specified `TO_TYPE` parameter type with no loss of information;
96/// otherwise, it is derived from `false_type`. By default, this
97/// metafunction yields `true_type` if `FROM_TYPE` is the same as `TO_TYPE`;
98/// else it yields `false_type`. `bdef_Function` should specialize this
99/// template to yield `true_type` when `FROM_TYPE` is an instantiation of
100/// `bdef_Function` and `TO_TYPE` is the corresponding instantiation of
101/// `bsl::function` with the same function prototype. Instantiation will
102/// fail if either `FROM_TYPE` or `TO_TYPE` are reference types.
103template <class FROM_TYPE, class TO_TYPE>
105: bsl::is_same<FROM_TYPE, TO_TYPE>::type {
106
107 // Force compilation failure if 'FROM_TYPE' or 'TO_TYPE' are a reference
108 // type.
111};
112
113/// Partial specialization of `Function_IsReferenceCompatible` for `TO_TYPE`
114/// being const. The evaluation is forwarded to other specializations after
115/// stripping the const qualifiers. Note that if `FROM_TYPE` is const and
116/// `TO_TYPE` is mutable, this partial specialization will not be selected
117/// and the resulting evaluation will yield `false_type`, reflecting the
118/// fact that a reference to const type cannot be substituted with a
119/// reference to mutable type.
120template <class FROM_TYPE, class TO_TYPE>
121struct Function_IsReferenceCompatible<FROM_TYPE, const TO_TYPE>
122: Function_IsReferenceCompatible<typename bsl::remove_const<FROM_TYPE>::type,
123 TO_TYPE> {
124};
125
126} // close package namespace
127
128
129#endif // !defined(INCLUDED_BSLSTL_FUNCTION_ISREFERENCECOMPATIBLE)
130
131// ----------------------------------------------------------------------------
132// Copyright 2020 Bloomberg Finance L.P.
133//
134// Licensed under the Apache License, Version 2.0 (the "License");
135// you may not use this file except in compliance with the License.
136// You may obtain a copy of the License at
137//
138// http://www.apache.org/licenses/LICENSE-2.0
139//
140// Unless required by applicable law or agreed to in writing, software
141// distributed under the License is distributed on an "AS IS" BASIS,
142// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143// See the License for the specific language governing permissions and
144// limitations under the License.
145// ----------------------------- END-OF-FILE ----------------------------------
146
147/** @} */
148/** @} */
149/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslstl_algorithm.h:82
Definition bslmf_issame.h:146
Definition bslmf_movableref.h:817
Definition bslstl_function_isreferencecompatible.h:105
BSLMF_ASSERT(!bslmf::MovableRefUtil::IsReference< TO_TYPE >::value)
BSLMF_ASSERT(!bslmf::MovableRefUtil::IsReference< FROM_TYPE >::value)