williamr@2: // (C) Copyright John Maddock 2000. williamr@2: // Use, modification and distribution are subject to the williamr@2: // Boost Software License, Version 1.0. (See accompanying file williamr@2: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) williamr@2: williamr@2: // See http://www.boost.org/libs/static_assert for documentation. williamr@2: williamr@2: /* williamr@2: Revision history: williamr@2: 02 August 2000 williamr@2: Initial version. williamr@2: */ williamr@2: williamr@2: #ifndef BOOST_STATIC_ASSERT_HPP williamr@2: #define BOOST_STATIC_ASSERT_HPP williamr@2: williamr@2: #include williamr@2: #include williamr@2: williamr@2: #ifdef __BORLANDC__ williamr@2: // williamr@2: // workaround for buggy integral-constant expression support: williamr@2: #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS williamr@2: #endif williamr@2: williamr@2: #if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4)) williamr@2: // gcc 3.3 and 3.4 don't produce good error messages with the default version: williamr@2: # define BOOST_SA_GCC_WORKAROUND williamr@2: #endif williamr@2: williamr@2: namespace boost{ williamr@2: williamr@2: // HP aCC cannot deal with missing names for template value parameters williamr@2: template struct STATIC_ASSERTION_FAILURE; williamr@2: williamr@2: template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; williamr@2: williamr@2: // HP aCC cannot deal with missing names for template value parameters williamr@2: template struct static_assert_test{}; williamr@2: williamr@2: } williamr@2: williamr@2: // williamr@2: // Implicit instantiation requires that all member declarations be williamr@2: // instantiated, but that the definitions are *not* instantiated. williamr@2: // williamr@2: // It's not particularly clear how this applies to enum's or typedefs; williamr@2: // both are described as declarations [7.1.3] and [7.2] in the standard, williamr@2: // however some compilers use "delayed evaluation" of one or more of williamr@2: // these when implicitly instantiating templates. We use typedef declarations williamr@2: // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum williamr@2: // version gets better results from your compiler... williamr@2: // williamr@2: // Implementation: williamr@2: // Both of these versions rely on sizeof(incomplete_type) generating an error williamr@2: // message containing the name of the incomplete type. We use williamr@2: // "STATIC_ASSERTION_FAILURE" as the type name here to generate williamr@2: // an eye catching error message. The result of the sizeof expression is either williamr@2: // used as an enum initialiser, or as a template argument depending which version williamr@2: // is in use... williamr@2: // Note that the argument to the assert is explicitly cast to bool using old- williamr@2: // style casts: too many compilers currently have problems with static_cast williamr@2: // when used inside integral constant expressions. williamr@2: // williamr@2: #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS) williamr@2: williamr@2: #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) williamr@2: // __LINE__ macro broken when -ZI is used see Q199057 williamr@2: // fortunately MSVC ignores duplicate typedef's. williamr@2: #define BOOST_STATIC_ASSERT( B ) \ williamr@2: typedef ::boost::static_assert_test<\ williamr@2: sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\ williamr@2: > boost_static_assert_typedef_ williamr@2: #elif defined(BOOST_MSVC) williamr@2: #define BOOST_STATIC_ASSERT( B ) \ williamr@2: typedef ::boost::static_assert_test<\ williamr@2: sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\ williamr@2: BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__) williamr@2: #elif defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND) williamr@2: // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error williamr@2: // instead of warning in case of failure williamr@2: # define BOOST_STATIC_ASSERT( B ) \ williamr@2: typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \ williamr@2: [ ::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >::value ] williamr@2: #elif defined(__sgi) williamr@2: // special version for SGI MIPSpro compiler williamr@2: #define BOOST_STATIC_ASSERT( B ) \ williamr@2: BOOST_STATIC_CONSTANT(bool, \ williamr@2: BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \ williamr@2: typedef ::boost::static_assert_test<\ williamr@2: sizeof(::boost::STATIC_ASSERTION_FAILURE< \ williamr@2: BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\ williamr@2: BOOST_JOIN(boost_static_assert_typedef_, __LINE__) williamr@2: #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003) williamr@2: // special version for CodeWarrior <= 8.x williamr@2: #define BOOST_STATIC_ASSERT( B ) \ williamr@2: BOOST_STATIC_CONSTANT(int, \ williamr@2: BOOST_JOIN(boost_static_assert_test_, __LINE__) = \ williamr@2: sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) ) williamr@2: #else williamr@2: // generic version williamr@2: #define BOOST_STATIC_ASSERT( B ) \ williamr@2: typedef ::boost::static_assert_test<\ williamr@2: sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\ williamr@2: BOOST_JOIN(boost_static_assert_typedef_, __LINE__) williamr@2: #endif williamr@2: williamr@2: #else williamr@2: // alternative enum based implementation: williamr@2: #define BOOST_STATIC_ASSERT( B ) \ williamr@2: enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \ williamr@2: = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) } williamr@2: #endif williamr@2: williamr@2: williamr@2: #endif // BOOST_STATIC_ASSERT_HPP williamr@2: williamr@2: