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