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