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>{};
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 #define BOOST_STATIC_ASSERT( B ) \
63 typedef ::boost::static_assert_test<\
64 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
65 BOOST_ASSERT_JOIN(boost_static_assert_typedef_, __LINE__)
67 // __LINE__ macro broken when -ZI is used see Q199057
68 // fortunately MSVC ignores duplicate typedef's.
69 #define BOOST_STATIC_ASSERT( B ) \
70 typedef ::boost::static_assert_test<\
71 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
72 > boost_static_assert_typedef_
75 // alternative enum based implementation:
76 #define BOOST_STATIC_ASSERT( B ) \
77 enum { BOOST_ASSERT_JOIN(boost_static_assert_enum_, __LINE__) \
78 = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
82 // The following piece of macro magic joins the two
83 // arguments together, even when one of the arguments is
84 // itself a macro (see 16.3.1 in C++ standard). The key
85 // is that macro expansion of macro arguments does not
86 // occur in BOOST_DO_ASSERT_JOIN but does in BOOST_ASSERT_JOIN
87 // provided it is called from within another macro.
88 #define BOOST_ASSERT_JOIN( X, Y ) BOOST_DO_ASSERT_JOIN( X, Y )
89 #define BOOST_DO_ASSERT_JOIN( X, Y ) X##Y
91 #endif // BOOST_STATIC_ASSERT_HPP