1 // (C) Copyright John Maddock 2000.
2 // Permission to copy, use, modify, sell and
3 // distribute this software is granted provided this copyright notice appears
4 // in all copies. This software is provided "as is" without express or implied
5 // warranty, and with no claim as to its suitability for any purpose.
7 // See http://www.boost.org for most recent version including documentation.
15 #ifndef BOOST_STATIC_ASSERT_HPP
16 #define BOOST_STATIC_ASSERT_HPP
18 #include <boost/config.hpp>
22 // workaround for buggy integral-constant expression support:
23 #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
28 // HP aCC cannot deal with missing names for template value parameters
29 template <bool x> struct STATIC_ASSERTION_FAILURE;
31 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
33 // HP aCC cannot deal with missing names for template value parameters
34 template<int x> struct static_assert_test{};
39 // Implicit instantiation requires that all member declarations be
40 // instantiated, but that the definitions are *not* instantiated.
42 // It's not particularly clear how this applies to enum's or typedefs;
43 // both are described as declarations [7.1.3] and [7.2] in the standard,
44 // however some compilers use "delayed evaluation" of one or more of
45 // these when implicitly instantiating templates. We use typedef declarations
46 // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
47 // version gets better results from your compiler...
50 // Both of these versions rely on sizeof(incomplete_type) generating an error
51 // message containing the name of the incomplete type. We use
52 // "STATIC_ASSERTION_FAILURE" as the type name here to generate
53 // an eye catching error message. The result of the sizeof expression is either
54 // used as an enum initialiser, or as a template argument depending which version
56 // Note that the argument to the assert is explicitly cast to bool using old-
57 // style casts: too many compilers currently have problems with static_cast
58 // when used inside integral constant expressions.
60 #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS) && !defined(__MWERKS__)
62 #if defined(BOOST_MSVC)
63 // __LINE__ macro broken when -ZI is used see Q199057
64 // fortunately MSVC ignores duplicate typedef's.
65 #define BOOST_STATIC_ASSERT( B ) \
66 typedef ::boost::static_assert_test<\
67 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
68 > boost_static_assert_typedef_
69 #elif defined(BOOST_INTEL_CXX_VERSION)
70 // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
71 // instead of warning in case of failure
72 # define BOOST_STATIC_ASSERT( B ) \
73 typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
74 [ ::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >::value ]
77 #define BOOST_STATIC_ASSERT( B ) \
78 typedef ::boost::static_assert_test<\
79 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
80 BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
84 // alternative enum based implementation:
85 #define BOOST_STATIC_ASSERT( B ) \
86 enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
87 = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
91 #endif // BOOST_STATIC_ASSERT_HPP