BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_addreference.h
Go to the documentation of this file.
1/// @file bslmf_addreference.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_addreference.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ADDREFERENCE
9#define INCLUDED_BSLMF_ADDREFERENCE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_addreference bslmf_addreference
15/// @brief Provide a meta-function for adding "reference-ness" to a type.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_addreference
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_addreference-purpose"> Purpose</a>
25/// * <a href="#bslmf_addreference-classes"> Classes </a>
26/// * <a href="#bslmf_addreference-description"> Description </a>
27/// * <a href="#bslmf_addreference-usage"> Usage </a>
28/// * <a href="#bslmf_addreference-example-1-a-simple-wrapper-class"> Example 1: A Simple Wrapper Class </a>
29/// * <a href="#bslmf_addreference-example-2-expected-results"> Example 2: Expected Results </a>
30///
31/// # Purpose {#bslmf_addreference-purpose}
32/// Provide a meta-function for adding "reference-ness" to a type.
33///
34/// # Classes {#bslmf_addreference-classes}
35///
36/// - bslmf::AddReference: meta-function to form a reference to a type
37///
38/// # Description {#bslmf_addreference-description}
39/// This component defines a simple template `struct`,
40/// `bslmf::AddReference`, that is used to define a reference type from the type
41/// supplied as its single template type parameter. Types that are `void` or
42/// already reference types are unmodified.
43///
44/// ## Usage {#bslmf_addreference-usage}
45///
46///
47/// This section illustrates intended use of this component.
48///
49/// ### Example 1: A Simple Wrapper Class {#bslmf_addreference-example-1-a-simple-wrapper-class}
50///
51///
52/// First, let us write a simple class that can wrap any other type:
53/// @code
54/// template <class t_TYPE>
55/// class Wrapper {
56/// private:
57/// // DATA
58/// t_TYPE d_data;
59///
60/// public:
61/// // TYPES
62/// typedef typename bslmf::AddReference<t_TYPE>::Type WrappedType;
63///
64/// // CREATORS
65/// Wrapper(t_TYPE value) : d_data(value) {} // IMPLICIT
66/// // Create a 'Wrapper' object having the specified 'value'.
67///
68/// //! ~Wrapper() = default;
69/// // Destroy this object.
70/// @endcode
71/// Then, we would like to expose access to the wrapped element through a method
72/// that returns a reference to the data member `d_data`. However, there would
73/// be a problem if the user supplied a parameterized type `t_TYPE` that is a
74/// reference type, as references-to-references were not permitted by the
75/// language (prior the C++11 standard). We can resolve such problems using the
76/// meta-function `bslmf::AddReference`.
77/// @code
78/// // MANIPULATORS
79/// typename bslmf::AddReference<t_TYPE>::Type value()
80/// {
81/// return d_data;
82/// }
83/// @endcode
84/// Next, we supply an accessor function, `value`, that similarly wraps the
85/// parameterized type `t_TYPE` with the `bslmf::AddReference` meta-function.
86/// In this case we must remember to const-quality `t_TYPE` before passing it on
87/// to the meta-function.
88/// @code
89/// // ACCESSORS
90/// typename bslmf::AddReference<const t_TYPE>::Type value() const
91/// {
92/// return d_data;
93/// }
94/// };
95/// @endcode
96/// Now, we write a test function, `runTest`, to verify our simple wrapper type.
97/// We start by wrapping a simple `int` value:
98/// @code
99/// void runTests()
100/// {
101/// int i = 42;
102///
103/// Wrapper<int> ti(i); const Wrapper<int>& TI = ti;
104/// assert(42 == i);
105/// assert(42 == TI.value());
106///
107/// ti.value() = 13;
108/// assert(42 == i);
109/// assert(13 == TI.value());
110/// @endcode
111/// Finally, we test `Wrapper` with a reference type:
112/// @code
113/// Wrapper<int&> tr(i); const Wrapper<int&>& TR = tr;
114/// assert(42 == i);
115/// assert(42 == TR.value());
116///
117/// tr.value() = 13;
118/// assert(13 == i);
119/// assert(13 == TR.value());
120///
121/// i = 42;
122/// assert(42 == i);
123/// assert(42 == TR.value());
124/// }
125/// @endcode
126///
127/// ### Example 2: Expected Results {#bslmf_addreference-example-2-expected-results}
128///
129///
130/// For this example, the associated comments below indicate the expected type
131/// of `bslmf::AddReference::Type` for a broad range of parameterized types:
132/// @code
133/// struct MyType {};
134/// typedef MyType& MyTypeRef;
135///
136/// bslmf::AddReference<int >::Type x1; // int&
137/// bslmf::AddReference<int& >::Type x2; // int&
138/// bslmf::AddReference<int volatile >::Type x3; // volatile int&
139/// bslmf::AddReference<int volatile& >::Type x4; // volatile int&
140///
141/// bslmf::AddReference<MyType >::Type // MyType&
142/// bslmf::AddReference<MyType& >::Type // MyType&
143/// bslmf::AddReference<MyTypeRef >::Type // MyType&
144/// bslmf::AddReference<MyType const >::Type // const MyType&
145/// bslmf::AddReference<MyType const& >::Type // const MyType&
146/// bslmf::AddReference<const MyTypeRef >::Type // MyType&
147/// bslmf::AddReference<const MyTypeRef&>::Type // MyType& (REQUIRES C++11)
148///
149/// bslmf::AddReference<void >::Type // void
150/// bslmf::AddReference<void * >::Type // void *&
151/// @endcode
152/// @}
153/** @} */
154/** @} */
155
156/** @addtogroup bsl
157 * @{
158 */
159/** @addtogroup bslmf
160 * @{
161 */
162/** @addtogroup bslmf_addreference
163 * @{
164 */
165
166#include <bslscm_version.h>
167
169
170
171
172namespace bslmf {
173
174 // ===================
175 // struct AddReference
176 // ===================
177
178/// This meta-function class defines a typedef, `Type`, that is an alias for
179/// a reference to the parameterized `t_TYPE`. References to cv-qualified
180/// `void` will produce the original `void` type and not a reference (see
181/// specializations below). References-to-references "collapse" to produce
182/// an alias to the original reference type, which is the revised rule
183/// according to the C++11 standard. Note that there is no requirement that
184/// the parameterized `t_TYPE` be a complete type.
185template <class t_TYPE>
190
191} // close package namespace
192
193#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
194// ============================================================================
195// BACKWARD COMPATIBILITY
196// ============================================================================
197
198#ifdef bslmf_AddReference
199#undef bslmf_AddReference
200#endif
201/// This alias is defined for backward compatibility.
202#define bslmf_AddReference bslmf::AddReference
203#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
204
205
206
207#endif
208
209// ----------------------------------------------------------------------------
210// Copyright 2013 Bloomberg Finance L.P.
211//
212// Licensed under the Apache License, Version 2.0 (the "License");
213// you may not use this file except in compliance with the License.
214// You may obtain a copy of the License at
215//
216// http://www.apache.org/licenses/LICENSE-2.0
217//
218// Unless required by applicable law or agreed to in writing, software
219// distributed under the License is distributed on an "AS IS" BASIS,
220// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
221// See the License for the specific language governing permissions and
222// limitations under the License.
223// ----------------------------- END-OF-FILE ----------------------------------
224
225/** @} */
226/** @} */
227/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlbb_blob.h:576
t_TYPE & type
This typedef defines the return type of this meta function.
Definition bslmf_addlvaluereference.h:129
Definition bslmf_addreference.h:186
bsl::add_lvalue_reference< t_TYPE >::type Type
Definition bslmf_addreference.h:188