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> 19 20 #ifndef BOOST_NO_STATIC_ASSERT 21 # define BOOST_STATIC_ASSERT_MSG( B, Msg ) static_assert(B, Msg) 22 #else 23 # define BOOST_STATIC_ASSERT_MSG( B, Msg ) BOOST_STATIC_ASSERT( B ) 24 #endif 25 26 #ifdef __BORLANDC__ 27 // 28 // workaround for buggy integral-constant expression support: 29 #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS 30 #endif 31 32 #if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4)) 33 // gcc 3.3 and 3.4 don't produce good error messages with the default version: 34 # define BOOST_SA_GCC_WORKAROUND 35 #endif 36 37 // 38 // If the compiler issues warnings about old C style casts, 39 // then enable this: 40 // 41 #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))) 42 # define BOOST_STATIC_ASSERT_BOOL_CAST( x ) ((x) == 0 ? false : true) 43 #else 44 # define BOOST_STATIC_ASSERT_BOOL_CAST(x) (bool)(x) 45 #endif 46 47 #ifndef BOOST_NO_STATIC_ASSERT 48 # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B) 49 #else 50 51 namespace boost{ 52 53 // HP aCC cannot deal with missing names for template value parameters 54 template <bool x> struct STATIC_ASSERTION_FAILURE; 55 56 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; }; 57 58 // HP aCC cannot deal with missing names for template value parameters 59 template<int x> struct static_assert_test{}; 60 61 } 62 63 // 64 // Implicit instantiation requires that all member declarations be 65 // instantiated, but that the definitions are *not* instantiated. 66 // 67 // It's not particularly clear how this applies to enum's or typedefs; 68 // both are described as declarations [7.1.3] and [7.2] in the standard, 69 // however some compilers use "delayed evaluation" of one or more of 70 // these when implicitly instantiating templates. We use typedef declarations 71 // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum 72 // version gets better results from your compiler... 73 // 74 // Implementation: 75 // Both of these versions rely on sizeof(incomplete_type) generating an error 76 // message containing the name of the incomplete type. We use 77 // "STATIC_ASSERTION_FAILURE" as the type name here to generate 78 // an eye catching error message. The result of the sizeof expression is either 79 // used as an enum initialiser, or as a template argument depending which version 80 // is in use... 81 // Note that the argument to the assert is explicitly cast to bool using old- 82 // style casts: too many compilers currently have problems with static_cast 83 // when used inside integral constant expressions. 84 // 85 #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS) 86 87 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) 88 // __LINE__ macro broken when -ZI is used see Q199057 89 // fortunately MSVC ignores duplicate typedef's. 90 #define BOOST_STATIC_ASSERT( B ) \ 91 typedef ::boost::static_assert_test<\ 92 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\ 93 > boost_static_assert_typedef_ 94 #elif defined(BOOST_MSVC) 95 #define BOOST_STATIC_ASSERT( B ) \ 96 typedef ::boost::static_assert_test<\ 97 sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST ( B ) >)>\ 98 BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__) 99 #elif defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND) 100 // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error 101 // instead of warning in case of failure 102 # define BOOST_STATIC_ASSERT( B ) \ 103 typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \ 104 [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >::value ] 105 #elif defined(__sgi) 106 // special version for SGI MIPSpro compiler 107 #define BOOST_STATIC_ASSERT( B ) \ 108 BOOST_STATIC_CONSTANT(bool, \ 109 BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \ 110 typedef ::boost::static_assert_test<\ 111 sizeof(::boost::STATIC_ASSERTION_FAILURE< \ 112 BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\ 113 BOOST_JOIN(boost_static_assert_typedef_, __LINE__) 114 #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003) 115 // special version for CodeWarrior <= 8.x 116 #define BOOST_STATIC_ASSERT( B ) \ 117 BOOST_STATIC_CONSTANT(int, \ 118 BOOST_JOIN(boost_static_assert_test_, __LINE__) = \ 119 sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >) ) 120 #else 121 // generic version 122 #define BOOST_STATIC_ASSERT( B ) \ 123 typedef ::boost::static_assert_test<\ 124 sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >)>\ 125 BOOST_JOIN(boost_static_assert_typedef_, __LINE__) 126 #endif 127 128 #else 129 // alternative enum based implementation: 130 #define BOOST_STATIC_ASSERT( B ) \ 131 enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \ 132 = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) } 133 #endif 134 #endif // defined(BOOST_NO_STATIC_ASSERT) 135 136 #endif // BOOST_STATIC_ASSERT_HPP 137 138 139