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)
6 // See http://www.boost.org/libs/static_assert for documentation.
14 #ifndef BOOST_STATIC_ASSERT_HPP
15 #define BOOST_STATIC_ASSERT_HPP
17 #include <boost/config.hpp>
18 #include <boost/detail/workaround.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) && \
61 !BOOST_WORKAROUND(__MWERKS__, < 0x3003)
63 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
64 // __LINE__ macro broken when -ZI is used see Q199057
65 // fortunately MSVC ignores duplicate typedef's.
66 #define BOOST_STATIC_ASSERT( B ) \
67 typedef ::boost::static_assert_test<\
68 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
69 > boost_static_assert_typedef_
70 #elif defined(BOOST_MSVC)
71 #define BOOST_STATIC_ASSERT( B ) \
72 typedef ::boost::static_assert_test<\
73 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
74 BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
75 #elif defined(BOOST_INTEL_CXX_VERSION)
76 // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
77 // instead of warning in case of failure
78 # define BOOST_STATIC_ASSERT( B ) \
79 typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
80 [ ::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >::value ]
82 // special version for SGI MIPSpro compiler
83 #define BOOST_STATIC_ASSERT( B ) \
84 BOOST_STATIC_CONSTANT(bool, \
85 BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \
86 typedef ::boost::static_assert_test<\
87 sizeof(::boost::STATIC_ASSERTION_FAILURE< \
88 BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\
89 BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
92 #define BOOST_STATIC_ASSERT( B ) \
93 typedef ::boost::static_assert_test<\
94 sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
95 BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
99 // alternative enum based implementation:
100 #define BOOST_STATIC_ASSERT( B ) \
101 enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
102 = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
106 #endif // BOOST_STATIC_ASSERT_HPP