BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_assert.h
Go to the documentation of this file.
1/// @file bslmf_assert.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_assert.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ASSERT
9#define INCLUDED_BSLMF_ASSERT
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_assert bslmf_assert
15/// @brief Provide a compile-time assertion facility.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_assert
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_assert-purpose"> Purpose</a>
25/// * <a href="#bslmf_assert-classes"> Classes </a>
26/// * <a href="#bslmf_assert-macros"> Macros </a>
27/// * <a href="#bslmf_assert-description"> Description </a>
28///
29/// # Purpose {#bslmf_assert-purpose}
30/// Provide a compile-time assertion facility.
31///
32/// # Classes {#bslmf_assert-classes}
33///
34///
35/// # Macros {#bslmf_assert-macros}
36///
37/// - BSLMF_ASSERT: compile-time assert macro
38///
39/// # Description {#bslmf_assert-description}
40/// This component defines a macro that will generate a
41/// compile-time error if its constant integral expression argument evaluates to
42/// false (i.e., a value of `0`). The `BSLMF_ASSERT` macro is similar to the
43/// standard run-time `assert` macro, except that its argument is evaluated at
44/// compile-time, it produces no executable code, and it can be used safely in
45/// header files.
46/// @code
47/// BSLMF_ASSERT(sizeof(int) >= sizeof(char)); // OK
48/// BSLMF_ASSERT(sizeof(double) < sizeof(char)); // COMPILE ERROR!
49/// @endcode
50/// The second line will result in a compile error with a message stating that
51/// the type `BSLMF_COMPILE_TIME_ASSERTION_FAILURE` is incomplete, usually
52/// preceded by the file and line number where the macro was used. The
53/// following error message is typical of most compilers:
54/// @code
55/// $ CC -g myfile.cpp
56/// "myfile.cpp", line 86: Error: The type
57/// "BSLMF_COMPILE_TIME_ASSERTION_FAILURE<0>" is incomplete.
58/// 1 Error(s) detected.
59/// @endcode
60/// If the macro argument is true, the macro will generate an innocuous
61/// `typedef` with a name that is the concatenation of the symbol
62/// `bslmf::Assert`, and the value of `__LINE__` (which will be the line number
63/// in the file where the macro was called). For example, the first line from
64/// the example above might result in the following statement:
65/// @code
66/// typedef bslmf::AssertTest<1> bslmf_Assert_85;
67/// @endcode
68/// Note that these generated typedefs are implementation details of the
69/// compile-time checking facility and are not intended to be used directly
70/// outside of this component.
71///
72/// Attempting to invoke `BSLMF_ASSERT` on a non-compile-time value will
73/// typically result in a compilation error.
74///
75/// `BSLMF_ASSERT` can be used at namespace, class, and function scope to assert
76/// compile-time conditions. !WARNING:! a compiler bug on certain platforms
77/// produces an error when the `BSLMF_ASSERT` macro is used more than once on
78/// the *same* line in class scope.
79/// @code
80/// +---------------------------------------------
81/// | // mytype.h
82/// | #ifndef INCLUDED_BSLMF_ASSERT
83/// | #include <bslmf_assert.h>
84/// | #endif
85/// |
86/// | class MyType {
87/// | BSLMF_ASSERT(sizeof(int) >= sizeof(char)); // OK
88/// | BSLMF_ASSERT(sizeof(int) >= sizeof(char)); // OK
89/// | BSLMF_ASSERT(1 == 1); // OK
90/// | BSLMF_ASSERT(1 == 1); BSLMF_ASSERT(1 == 1); // SAME LINE - MIGHT
91/// | // CAUSE ERROR!
92/// |
93/// | int d_data;
94/// | ...
95/// | void foo();
96/// | ...
97/// | };
98///
99/// +---------------------------------------------
100/// | // mytype.cpp
101/// | #include <mytype.h>
102/// | #include <bslmf_assert.h>
103/// |
104/// | BSLMF_ASSERT(sizeof(int) >= sizeof(char));
105/// |
106/// | void MyType::foo()
107/// | {
108/// | BSLMF_ASSERT(sizeof(int) >= sizeof(char));
109/// | ...
110/// | }
111/// @endcode
112/// @}
113/** @} */
114/** @} */
115
116/** @addtogroup bsl
117 * @{
118 */
119/** @addtogroup bslmf
120 * @{
121 */
122/** @addtogroup bslmf_assert
123 * @{
124 */
125
126#include <bslscm_version.h>
127
128#include <bsla_maybeunused.h>
129
131
133#include <bsls_platform.h>
134
135#ifdef BSLS_COMPILERFEATURES_SUPPORT_STATIC_ASSERT
136#define BSLMF_ASSERT(BSLMF_CONSTANT_EXPRESSION) \
137 static_assert((BSLMF_CONSTANT_EXPRESSION), #BSLMF_CONSTANT_EXPRESSION)
138#else
139
140
141
142 // ==============
143 // Support macros
144 // ==============
145
146#define BSLMF_ASSERT_CAT(X, Y) BSLMF_ASSERT_CAT_IMP1(X, Y)
147#define BSLMF_ASSERT_CAT_IMP1(X, Y) BSLMF_ASSERT_CAT_IMP2(X, Y)
148#define BSLMF_ASSERT_CAT_IMP2(X, Y) X##Y
149
150 // =============
151 // Support types
152 // =============
153
154#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
155
156// Using a different implementation on Sun; see BSLMF_ASSERT for details.
157
158namespace bslmf {
159
160struct Assert_TrueType {
162};
163
164struct Assert_FalseType {
165};
166
167template <bool COND>
168struct Assert_If : Assert_TrueType {
169};
170
171template <>
172struct Assert_If<false> : Assert_FalseType {
173};
174
175} // close package namespace
176
177#else
178
179/// Declared but not defined. If assert macro references this type, then
180/// compilation will fail (assert failure).
181template <bool IS_VALID>
183
184/// Specialization for value 1 (true). Referencing this specialization will
185/// allow compilation to succeed (assert succeeded).
186template <>
189
190namespace bslmf {
191
192/// Instantiating this type involves instantiating its template parameter.
193/// This dummy type is just used to force instantiation of a meta-function
194/// used as its argument.
195template <bool IS_VALID>
197};
198
199} // close package namespace
200
201#endif
202
203 // ==================
204 // macro BSLMF_ASSERT
205 // ==================
206
207#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
208
209// The usual definition of the 'BSLMF_ASSERT' macro doesn't work with SunCC
210// (version 10 and below) inside template classes. Note that Sun CC has a
211// quite non-conformant (read 'broken') template instantiation mechanism. See
212// DRQS 29636421 for an example of code Sun CC didn't compile correctly with
213// the usual definition of 'BSLMF_ASSERT'. Below is the definition that works
214// more reliably. This definition is not well-formed, it just happens to work
215// with SunCC. So don't use it with other compilers.
216
217#define BSLMF_ASSERT(expr) \
218 struct BSLMF_ASSERT_CAT(bslmf_Assert_, __LINE__) \
219 : ::BloombergLP::bslmf::Assert_If<!!(int)(expr)> \
220 { \
221 BSLMF_COMPILE_TIME_ASSERTION_FAILURE * dummy; \
222 }; \
223 \
224 enum { BSLMF_ASSERT_CAT(bslmf_Assert_Check_, __LINE__) \
225 = sizeof(BSLMF_ASSERT_CAT(bslmf_Assert_, __LINE__)) } \
226
227#else
228
229#define BSLMF_ASSERT(expr) \
230BSLA_MAYBE_UNUSED typedef BloombergLP::bslmf::AssertTest< \
231 sizeof(BloombergLP::BSLMF_COMPILE_TIME_ASSERTION_FAILURE<!!(expr)>)> \
232 BSLMF_ASSERT_CAT(bslmf_Assert_, __LINE__)
233#endif
234
235
236
237#endif // BSLS_COMPILERFEATURES_SUPPORT_STATIC_ASSERT
238
239#endif
240
241// ----------------------------------------------------------------------------
242// Copyright 2013 Bloomberg Finance L.P.
243//
244// Licensed under the Apache License, Version 2.0 (the "License");
245// you may not use this file except in compliance with the License.
246// You may obtain a copy of the License at
247//
248// http://www.apache.org/licenses/LICENSE-2.0
249//
250// Unless required by applicable law or agreed to in writing, software
251// distributed under the License is distributed on an "AS IS" BASIS,
252// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
253// See the License for the specific language governing permissions and
254// limitations under the License.
255// ----------------------------- END-OF-FILE ----------------------------------
256
257/** @} */
258/** @} */
259/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlbb_blob.h:576
Definition bslmf_assert.h:182
Definition bslmf_assert.h:196