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)
6 // See http://www.boost.org/libs/static_assert for documentation.
14 #ifndef BOOST_STATIC_ASSERT_HPP
15 #define BOOST_STATIC_ASSERT_HPP
17 #include <boost/config.hpp>
18 #include <boost/detail/workaround.hpp>
19 #include <cstddef> //for std::size_t
21 #if defined(__GNUC__) && !defined(__GXX_EXPERIMENTAL_CXX0X__)
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.
27 #pragma GCC system_header
30 #ifndef BOOST_NO_CXX11_STATIC_ASSERT
31 # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
32 # define BOOST_STATIC_ASSERT_MSG( ... ) static_assert(__VA_ARGS__)
34 # define BOOST_STATIC_ASSERT_MSG( B, Msg ) static_assert( B, Msg )
37 # define BOOST_STATIC_ASSERT_MSG( B, Msg ) BOOST_STATIC_ASSERT( B )
42 // workaround for buggy integral-constant expression support:
43 #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
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
52 // If the compiler issues warnings about old C style casts,
55 #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
56 # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
57 # define BOOST_STATIC_ASSERT_BOOL_CAST( ... ) ((__VA_ARGS__) != 0)
59 # define BOOST_STATIC_ASSERT_BOOL_CAST( x ) ((x) != 0)
62 # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
63 # define BOOST_STATIC_ASSERT_BOOL_CAST( ... ) (bool)(__VA_ARGS__)
65 # define BOOST_STATIC_ASSERT_BOOL_CAST(x) (bool)(x)
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__)
73 # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B)
79 // HP aCC cannot deal with missing names for template value parameters
80 template <bool x> struct STATIC_ASSERTION_FAILURE;
82 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
84 // HP aCC cannot deal with missing names for template value parameters
85 template<std::size_t x> struct static_assert_test{};
90 // Implicit instantiation requires that all member declarations be
91 // instantiated, but that the definitions are *not* instantiated.
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...
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
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.
111 #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS)
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 ]
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 ) >) )
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
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
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__ ) >) }
172 # define BOOST_STATIC_ASSERT(B) \
173 enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
174 = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
177 #endif // defined(BOOST_NO_CXX11_STATIC_ASSERT)
179 #endif // BOOST_STATIC_ASSERT_HPP