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