BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_cpp11.h
Go to the documentation of this file.
1
/// @file bsls_cpp11.h
2
///
3
/// The content of this file has been pre-processed for Doxygen.
4
///
5
6
7
// bsls_cpp11.h -*-C++-*-
8
#ifndef INCLUDED_BSLS_CPP11
9
#define INCLUDED_BSLS_CPP11
10
11
#include <
bsls_ident.h
>
12
BSLS_IDENT
(
"$Id: $"
)
13
14
/// @defgroup bsls_cpp11 bsls_cpp11
15
/// @brief Provide macros for C++11 forward compatibility.
16
/// @addtogroup bsl
17
/// @{
18
/// @addtogroup bsls
19
/// @{
20
/// @addtogroup bsls_cpp11
21
/// @{
22
///
23
/// <h1> Outline </h1>
24
/// * <a href="#bsls_cpp11-purpose"> Purpose</a>
25
/// * <a href="#bsls_cpp11-classes"> Classes </a>
26
/// * <a href="#bsls_cpp11-macros"> Macros </a>
27
/// * <a href="#bsls_cpp11-description"> Description </a>
28
/// * <a href="#bsls_cpp11-macro-summary"> Macro Summary </a>
29
/// * <a href="#bsls_cpp11-usage"> Usage </a>
30
/// * <a href="#bsls_cpp11-example-1-preparing-c-03-code-for-c-11-features"> Example 1: Preparing C++03 Code for C++11 Features </a>
31
///
32
/// # Purpose {#bsls_cpp11-purpose}
33
/// Provide macros for C++11 forward compatibility.
34
///
35
/// @deprecated Use @ref bsls_keyword instead.
36
///
37
/// # Classes {#bsls_cpp11-classes}
38
///
39
///
40
/// # Macros {#bsls_cpp11-macros}
41
///
42
/// - BSLS_CPP11_CONSTEXPR: C++11 `constexpr` keyword
43
/// - BSLS_CPP11_DELETED: C++11 `= delete` function definition
44
/// - BSLS_CPP11_EXPLICIT: C++11 `explicit` for conversion operators
45
/// - BSLS_CPP11_FINAL: C++11 `final` keyword
46
/// - BSLS_CPP11_NOEXCEPT: C++11 `noexcept` keyword
47
/// - BSLS_CPP11_NOEXCEPT_AVAILABLE: `C++11` `noexcept` flag
48
/// - BSLS_CPP11_NOEXCEPT_SPECIFICATION(...): C++11 `noexcept` function qualifier
49
/// - BSLS_CPP11_NOEXCEPT_OPERATOR(expr): C++11 `noexcept` operation
50
/// - BSLS_CPP11_OVERRIDE: C++11 `override` keyword
51
/// - BSLS_CPP11_PROVISIONALLY_FALSE: C++11 specification placeholder
52
///
53
/// # Description {#bsls_cpp11-description}
54
/// This component provides definitions to use C++11 features in
55
/// both C++03 and C++11 without using conditional compilation where the
56
/// features are used. The goal is to allow implementation of components such
57
/// that they can take advantage of some C++11 features when compiled with C++11
58
/// mode enabled while also correctly compiling in C++03 mode. The
59
/// functionality of the respective features won't be available in C++03 mode.
60
///
61
/// ## Macro Summary {#bsls_cpp11-macro-summary}
62
///
63
///
64
/// The following are the macros provided by this component.
65
///
66
/// `BSLS_CPP11_CONSTEXPR`:
67
/// **DEPRECATED**: Use `BSLS_KEYWORD_CONSTEXPR` instead. This macro inserts
68
/// the keyword `constexpr` when compiling with C++11 mode and inserts
69
/// nothing when compiling with C++03 mode.
70
///
71
/// `BSLS_CPP11_DELETED`:
72
/// **DEPRECATED**: Use `BSLS_KEYWORD_DELETED` instead. This macro inserts
73
/// the text `= delete` when compiling with C++11 mode and inserts nothing
74
/// when compiling with C++03 mode.
75
///
76
/// `BSLS_CPP11_EXPLICIT`:
77
/// **DEPRECATED**: Use `BSLS_KEYWORD_EXPLICIT` instead. This macro inserts
78
/// the keyword `explicit` when compiling with C++11 mode and inserts
79
/// nothing when compiling with C++03 mode.
80
///
81
/// `BSLS_CPP11_FINAL`:
82
/// **DEPRECATED**: Use `BSLS_KEYWORD_FINAL` instead. This macro inserts the
83
/// keyword `final` when compiling with C++11 mode and inserts nothing when
84
/// compiling with C++03 mode.
85
///
86
/// `BSLS_CPP11_NOEXCEPT`:
87
/// **DEPRECATED**: Use `BSLS_KEYWORD_NOEXCEPT` instead. This macro inserts
88
/// the keyword `noexcept` when compiling with C++11 mode and inserts
89
/// nothing when compiling with C++03 mode.
90
///
91
/// `BSLS_CPP11_NOEXCEPT_AVAILABLE`:
92
/// **DEPRECATED**: Use `BSLS_KEYWORD_NOEXCEPT_AVAILABLE` instead. This
93
/// macro expands to `true` when the `noexcept` feature is available and
94
/// `false` otherwise.
95
///
96
/// `BSLS_CPP11_NOEXCEPT_SPECIFICATION(BOOL_EXPRESSION)`:
97
/// **DEPRECATED**: Use `BSLS_KEYWORD_NOEXCEPT_SPECIFICATION` instead. This
98
/// macro inserts the exception specification `noexcept(BOOL_EXPRESSION)`
99
/// when compiling with C++11 mode and inserts nothing when compiling with
100
/// C++03 mode. This macro is used to specify which version of `noexcept`
101
/// is intended when multiple `noexcept`s are used in a single statement.
102
///
103
/// `BSLS_CPP11_NOEXCEPT_OPERATOR(expr)`:
104
/// **DEPRECATED**: Use `BSLS_KEYWORD_NOEXCEPT_OPERATOR` instead. This macro
105
/// inserts the operation `noexcept(expr)` when compiling with C++11 mode
106
/// and inserts the literal `false` when compiling with C++03 mode.
107
///
108
/// `BSLS_CPP11_OVERRIDE`
109
/// **DEPRECATED**: Use `BSLS_KEYWORD_OVERRIDE` instead. This macro inserts
110
/// the keyword `override` when compiling with C++11 mode and inserts
111
/// nothing when compiling with C++03 mode.
112
///
113
/// `BSLS_CPP11_PROVISIONALLY_FALSE`:
114
/// **DEPRECATED**: Use `false` instead. This macro inserts the keyword
115
/// `false`. This macro is intended to be used as a placeholder in the
116
/// `BSLS_CPP11_NOEXCEPT_SPECIFICATION(BOOL_EXPRESSION)` macro when
117
/// traits needed for `BOOL_EXPRESSION` have not yet been implemented.
118
///
119
/// ## Usage {#bsls_cpp11-usage}
120
///
121
///
122
/// This section illustrates intended use of this component.
123
///
124
/// ### Example 1: Preparing C++03 Code for C++11 Features {#bsls_cpp11-example-1-preparing-c-03-code-for-c-11-features}
125
///
126
///
127
/// To use these macros, simply insert them where the corresponding C++11
128
/// keyword would go. When compiling with C++03 mode there will be no effect
129
/// but when compiling with C++11 mode additional restrictions will apply. When
130
/// compiling with C++11 mode the restriction will be checked providing some
131
/// additional checking over what is done with C++11.
132
///
133
/// C++ uses the `explicit` keyword to indicate that constructors taking just
134
/// one argument are not considered for implicit conversions. Instead, they can
135
/// only be used for explicit conversions. C++ also provides the ability to
136
/// define conversion operators but prior to C++11 these conversion operators
137
/// are considered for implicit conversion. C++11 allows the use of the
138
/// `explicit` keyword with conversion operators to avoid its use for implicit
139
/// conversions. The macro `BSLS_CPP11_EXPLICIT` can be used to mark
140
/// conversions as explicit conversions which will be checked when compiling
141
/// with C++11 mode. For example, an `Optional` type may have an explicit
142
/// conversion to `bool` to indicate that the value is set (note the conversion
143
/// operator):
144
/// @code
145
/// template <class TYPE>
146
/// class Optional
147
/// {
148
/// TYPE* d_value;
149
/// public:
150
/// Optional(): d_value() {}
151
/// explicit Optional(const TYPE& value): d_value(new TYPE(value)) {}
152
/// ~Optional() { delete d_value; }
153
/// // ...
154
///
155
/// BSLS_CPP11_EXPLICIT operator bool() const { return d_value; }
156
/// };
157
/// @endcode
158
/// When using an object of the `Optional` class in a condition it is desirable
159
/// that it converts to a `bool`:
160
/// @code
161
/// Optional<int> value;
162
/// if (value) { /*... */ }
163
/// @endcode
164
/// In places where an implicit conversion takes place it is not desirable that
165
/// the conversion is used. When compiling with C++11 mode the conversion
166
/// operator will not be used, e.g., the following code will result in an error:
167
/// @code
168
/// bool flag = value;
169
/// @endcode
170
/// When defining conversion operators to `bool` for code which needs to compile
171
/// with C++03 mode the conversion operator should convert to a member pointer
172
/// type instead: doing so has a similar effect to making the conversion
173
/// operator `explicit`.
174
///
175
/// Some classes are not intended for use as a base class. To clearly label
176
/// these classes and enforce that they can't be derived from C++11 allows using
177
/// the `final` keyword after the class name in the class definition to label
178
/// classes which are not intended to be derived from. The macro
179
/// `BSLS_CPP11_FINAL` is replaced by `final` when compiling with C++11 causing
180
/// the compiler to enforce that a class can't be further derived. The code
181
/// below defines a class which can't be derived from:
182
/// @code
183
/// class FinalClass BSLS_CPP11_FINAL
184
/// {
185
/// int d_value;
186
/// public:
187
/// explicit FinalClass(int value = 0): d_value(value) {}
188
/// int value() const { return d_value; }
189
/// };
190
/// @endcode
191
/// An attempt to derive from this class will fail when compiling with C++11
192
/// mode:
193
/// @code
194
/// class FinalClassDerived: public FinalClass {
195
/// int d_anotherValue;
196
/// public:
197
/// explicit FinalClassDerived(int value)
198
/// : d_anotherValue(2 * value) {
199
/// }
200
/// int anotherValue() const { return d_anotherValue; }
201
/// };
202
/// @endcode
203
/// The code will compile successfully when using C++03 mode.
204
///
205
/// Sometime it is useful to declare that an overriding function is the final
206
/// overriding function and further derived classes won't be allowed to further
207
/// override the function. One use of this feature could be informing the
208
/// compiler that it won't need to use virtual dispatch when calling this
209
/// function on a pointer or a reference of the corresponding type. C++11
210
/// allows marking functions as the final overrider using the keyword `final`.
211
/// The macro `BSLS_CPP11_FINAL` can also be used for this purpose. To
212
/// demonstrate the use of this keyword first a base class with a `virtual`
213
/// function is defined:
214
/// @code
215
/// struct FinalFunctionBase
216
/// {
217
/// virtual int f() { return 0; }
218
/// };
219
/// @endcode
220
/// When defining a derived class this function `f` can be marked as the final
221
/// overrider using `BSLS_CPP11_FINAL`:
222
/// @code
223
/// struct FinalFunctionDerived: FinalFunctionBase
224
/// {
225
/// int f() BSLS_CPP11_FINAL { return 1; }
226
/// };
227
/// @endcode
228
/// The semantics of the overriding function aren't changed but a further
229
/// derived class can't override the function `f`, i.e., the following code will
230
/// result in an error when compiling with C++11 mode:
231
/// @code
232
/// struct FinalFunctionFailure: FinalFunctionDerived
233
/// {
234
/// int f() { return 2; }
235
/// };
236
/// @endcode
237
/// With C++03 mode the code will successfully compile.
238
///
239
/// The C++11 keyword `override` is used to identify functions overriding a
240
/// `virtual` function from a base class. If a function identified as
241
/// `override` does not override a `virtual` function from a base class the
242
/// compilation results in an error. The macro `BSLS_CPP11_OVERRIDE` is used to
243
/// insert the `override` keyword when compiling with C++11 mode. When
244
/// compiling with C++03 mode it has no effect but it both cases it documents
245
/// that a function is overriding a `virtual` function from a base class. To
246
/// demonstrate the use of the `BSLS_CPP11_OVERRIDE` macro first a base class is
247
/// defined:
248
/// @code
249
/// struct OverrideBase
250
/// {
251
/// virtual int f() const { return 0; }
252
/// };
253
/// @endcode
254
/// When overriding `OverrideBase::f` in a derived class the
255
/// `BSLS_CPP11_OVERRIDE` macro should be used to ascertain that the function in
256
/// the derived class is indeed overriding a `virtual` function:
257
/// @code
258
/// struct OverrideSuccess: OverrideBase
259
/// {
260
/// int f() const BSLS_CPP11_OVERRIDE { return 1; }
261
/// };
262
/// @endcode
263
/// The above code compiles successfully with both C++03 mode and C++11. When
264
/// the function meant to be an override actually isn't overriding any function
265
/// the compilation will fail when using C++11 mode as is demonstrated by the
266
/// following example (note the missing `const` in the function declaration):
267
/// @code
268
/// struct OverrideFailure: OverrideBase
269
/// {
270
/// int f() BSLS_CPP11_OVERRIDE { return 2; }
271
/// };
272
/// @endcode
273
/// @}
274
/** @} */
275
/** @} */
276
277
/** @addtogroup bsl
278
* @{
279
*/
280
/** @addtogroup bsls
281
* @{
282
*/
283
/** @addtogroup bsls_cpp11
284
* @{
285
*/
286
287
#include <bsls_keyword.h>
288
289
#define BSLS_CPP11_CONSTEXPR BSLS_KEYWORD_CONSTEXPR
290
291
#define BSLS_CPP11_DELETED BSLS_KEYWORD_DELETED
292
293
#define BSLS_CPP11_NOEXCEPT BSLS_KEYWORD_NOEXCEPT
294
#define BSLS_CPP11_NOEXCEPT_AVAILABLE BSLS_KEYWORD_NOEXCEPT_AVAILABLE
295
#define BSLS_CPP11_NOEXCEPT_SPECIFICATION(...) \
296
BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(__VA_ARGS__)
297
#define BSLS_CPP11_NOEXCEPT_OPERATOR(...) \
298
BSLS_KEYWORD_NOEXCEPT_OPERATOR(__VA_ARGS__)
299
#define BSLS_CPP11_PROVISIONALLY_FALSE false
300
301
#define BSLS_CPP11_EXPLICIT BSLS_KEYWORD_EXPLICIT
302
303
#define BSLS_CPP11_FINAL BSLS_KEYWORD_FINAL
304
305
#define BSLS_CPP11_OVERRIDE BSLS_KEYWORD_OVERRIDE
306
307
// ----------------------------------------------------------------------------
308
309
#endif
310
311
// ----------------------------------------------------------------------------
312
// Copyright 2015 Bloomberg Finance L.P.
313
//
314
// Licensed under the Apache License, Version 2.0 (the "License");
315
// you may not use this file except in compliance with the License.
316
// You may obtain a copy of the License at
317
//
318
// http://www.apache.org/licenses/LICENSE-2.0
319
//
320
// Unless required by applicable law or agreed to in writing, software
321
// distributed under the License is distributed on an "AS IS" BASIS,
322
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
323
// See the License for the specific language governing permissions and
324
// limitations under the License.
325
// ----------------------------- END-OF-FILE ----------------------------------
326
327
/** @} */
328
/** @} */
329
/** @} */
bsls_ident.h
BSLS_IDENT
#define BSLS_IDENT(str)
Definition
bsls_ident.h:195
doxygen_input
bde
groups
bsl
bsls
bsls_cpp11.h
Generated by
1.9.8