]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright John Maddock 2000. |
2 | // Use, modification and distribution are subject to the | |
3 | // Boost Software License, Version 1.0. (See accompanying file | |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | // See http://www.boost.org/libs/static_assert for documentation. | |
7 | ||
8 | /* | |
9 | Revision history: | |
10 | 02 August 2000 | |
11 | Initial version. | |
12 | */ | |
13 | ||
14 | #ifndef BOOST_STATIC_ASSERT_HPP | |
15 | #define BOOST_STATIC_ASSERT_HPP | |
16 | ||
17 | #include <boost/config.hpp> | |
18 | #include <boost/detail/workaround.hpp> | |
1e59de90 | 19 | #include <cstddef> //for std::size_t |
7c673cae FG |
20 | |
21 | #if defined(__GNUC__) && !defined(__GXX_EXPERIMENTAL_CXX0X__) | |
22 | // | |
23 | // This is horrible, but it seems to be the only we can shut up the | |
24 | // "anonymous variadic macros were introduced in C99 [-Wvariadic-macros]" | |
25 | // warning that get spewed out otherwise in non-C++11 mode. | |
26 | // | |
27 | #pragma GCC system_header | |
28 | #endif | |
29 | ||
30 | #ifndef BOOST_NO_CXX11_STATIC_ASSERT | |
31 | # ifndef BOOST_NO_CXX11_VARIADIC_MACROS | |
32 | # define BOOST_STATIC_ASSERT_MSG( ... ) static_assert(__VA_ARGS__) | |
33 | # else | |
34 | # define BOOST_STATIC_ASSERT_MSG( B, Msg ) static_assert( B, Msg ) | |
35 | # endif | |
36 | #else | |
37 | # define BOOST_STATIC_ASSERT_MSG( B, Msg ) BOOST_STATIC_ASSERT( B ) | |
38 | #endif | |
39 | ||
20effc67 | 40 | #ifdef BOOST_BORLANDC |
7c673cae FG |
41 | // |
42 | // workaround for buggy integral-constant expression support: | |
43 | #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS | |
44 | #endif | |
45 | ||
46 | #if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4)) | |
47 | // gcc 3.3 and 3.4 don't produce good error messages with the default version: | |
48 | # define BOOST_SA_GCC_WORKAROUND | |
49 | #endif | |
50 | ||
51 | // | |
52 | // If the compiler issues warnings about old C style casts, | |
53 | // then enable this: | |
54 | // | |
55 | #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))) | |
56 | # ifndef BOOST_NO_CXX11_VARIADIC_MACROS | |
92f5a8d4 | 57 | # define BOOST_STATIC_ASSERT_BOOL_CAST( ... ) ((__VA_ARGS__) != 0) |
7c673cae | 58 | # else |
92f5a8d4 | 59 | # define BOOST_STATIC_ASSERT_BOOL_CAST( x ) ((x) != 0) |
7c673cae FG |
60 | # endif |
61 | #else | |
62 | # ifndef BOOST_NO_CXX11_VARIADIC_MACROS | |
63 | # define BOOST_STATIC_ASSERT_BOOL_CAST( ... ) (bool)(__VA_ARGS__) | |
64 | # else | |
65 | # define BOOST_STATIC_ASSERT_BOOL_CAST(x) (bool)(x) | |
66 | # endif | |
67 | #endif | |
68 | ||
69 | #ifndef BOOST_NO_CXX11_STATIC_ASSERT | |
70 | # ifndef BOOST_NO_CXX11_VARIADIC_MACROS | |
71 | # define BOOST_STATIC_ASSERT( ... ) static_assert(__VA_ARGS__, #__VA_ARGS__) | |
72 | # else | |
73 | # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B) | |
74 | # endif | |
75 | #else | |
76 | ||
77 | namespace boost{ | |
78 | ||
79 | // HP aCC cannot deal with missing names for template value parameters | |
80 | template <bool x> struct STATIC_ASSERTION_FAILURE; | |
81 | ||
82 | template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; }; | |
83 | ||
84 | // HP aCC cannot deal with missing names for template value parameters | |
1e59de90 | 85 | template<std::size_t x> struct static_assert_test{}; |
7c673cae FG |
86 | |
87 | } | |
88 | ||
89 | // | |
90 | // Implicit instantiation requires that all member declarations be | |
91 | // instantiated, but that the definitions are *not* instantiated. | |
92 | // | |
93 | // It's not particularly clear how this applies to enum's or typedefs; | |
94 | // both are described as declarations [7.1.3] and [7.2] in the standard, | |
95 | // however some compilers use "delayed evaluation" of one or more of | |
96 | // these when implicitly instantiating templates. We use typedef declarations | |
97 | // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum | |
98 | // version gets better results from your compiler... | |
99 | // | |
100 | // Implementation: | |
101 | // Both of these versions rely on sizeof(incomplete_type) generating an error | |
102 | // message containing the name of the incomplete type. We use | |
103 | // "STATIC_ASSERTION_FAILURE" as the type name here to generate | |
104 | // an eye catching error message. The result of the sizeof expression is either | |
105 | // used as an enum initialiser, or as a template argument depending which version | |
106 | // is in use... | |
107 | // Note that the argument to the assert is explicitly cast to bool using old- | |
108 | // style casts: too many compilers currently have problems with static_cast | |
109 | // when used inside integral constant expressions. | |
110 | // | |
111 | #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS) | |
112 | ||
113 | #if defined(BOOST_MSVC) && defined(BOOST_NO_CXX11_VARIADIC_MACROS) | |
114 | #define BOOST_STATIC_ASSERT( B ) \ | |
115 | typedef ::boost::static_assert_test<\ | |
116 | sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST ( B ) >)>\ | |
117 | BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__) | |
118 | #elif defined(BOOST_MSVC) | |
119 | #define BOOST_STATIC_ASSERT(...) \ | |
120 | typedef ::boost::static_assert_test<\ | |
121 | sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST (__VA_ARGS__) >)>\ | |
122 | BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__) | |
123 | #elif (defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)) && defined(BOOST_NO_CXX11_VARIADIC_MACROS) | |
124 | // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error | |
125 | // instead of warning in case of failure | |
126 | # define BOOST_STATIC_ASSERT( B ) \ | |
127 | typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \ | |
128 | [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >::value ] | |
129 | #elif (defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)) && !defined(BOOST_NO_CXX11_VARIADIC_MACROS) | |
130 | // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error | |
131 | // instead of warning in case of failure | |
132 | # define BOOST_STATIC_ASSERT(...) \ | |
133 | typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \ | |
134 | [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >::value ] | |
135 | #elif defined(__sgi) | |
136 | // special version for SGI MIPSpro compiler | |
137 | #define BOOST_STATIC_ASSERT( B ) \ | |
138 | BOOST_STATIC_CONSTANT(bool, \ | |
139 | BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \ | |
140 | typedef ::boost::static_assert_test<\ | |
141 | sizeof(::boost::STATIC_ASSERTION_FAILURE< \ | |
142 | BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\ | |
143 | BOOST_JOIN(boost_static_assert_typedef_, __LINE__) | |
144 | #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003) | |
145 | // special version for CodeWarrior <= 8.x | |
146 | #define BOOST_STATIC_ASSERT( B ) \ | |
147 | BOOST_STATIC_CONSTANT(int, \ | |
148 | BOOST_JOIN(boost_static_assert_test_, __LINE__) = \ | |
149 | sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >) ) | |
150 | #else | |
151 | // generic version | |
152 | # ifndef BOOST_NO_CXX11_VARIADIC_MACROS | |
153 | # define BOOST_STATIC_ASSERT( ... ) \ | |
154 | typedef ::boost::static_assert_test<\ | |
155 | sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >)>\ | |
156 | BOOST_JOIN(boost_static_assert_typedef_, __LINE__) BOOST_ATTRIBUTE_UNUSED | |
157 | # else | |
158 | # define BOOST_STATIC_ASSERT( B ) \ | |
159 | typedef ::boost::static_assert_test<\ | |
160 | sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >)>\ | |
161 | BOOST_JOIN(boost_static_assert_typedef_, __LINE__) BOOST_ATTRIBUTE_UNUSED | |
162 | # endif | |
163 | #endif | |
164 | ||
165 | #else | |
166 | // alternative enum based implementation: | |
167 | # ifndef BOOST_NO_CXX11_VARIADIC_MACROS | |
168 | # define BOOST_STATIC_ASSERT( ... ) \ | |
169 | enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \ | |
170 | = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( __VA_ARGS__ ) >) } | |
171 | # else | |
172 | # define BOOST_STATIC_ASSERT(B) \ | |
173 | enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \ | |
174 | = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) } | |
175 | # endif | |
176 | #endif | |
177 | #endif // defined(BOOST_NO_CXX11_STATIC_ASSERT) | |
178 | ||
179 | #endif // BOOST_STATIC_ASSERT_HPP | |
180 | ||
181 |