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