Overview   API Reference  

contrib/boost/static_assert.hpp

00001 //  (C) Copyright John Maddock 2000.
00002 //  Use, modification and distribution are subject to the 
00003 //  Boost Software License, Version 1.0. (See accompanying file 
00004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00005 
00006 //  See http://www.boost.org/libs/static_assert for documentation.
00007 
00008 /*
00009  Revision history:
00010    02 August 2000
00011       Initial version.
00012 */
00013 
00014 #ifndef BOOST_STATIC_ASSERT_HPP
00015 #define BOOST_STATIC_ASSERT_HPP
00016 
00017 #include <boost/config.hpp>
00018 #include <boost/detail/workaround.hpp>
00019 
00020 #ifdef __BORLANDC__
00021 //
00022 // workaround for buggy integral-constant expression support:
00023 #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
00024 #endif
00025 
00026 #if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4))
00027 // gcc 3.3 and 3.4 don't produce good error messages with the default version:
00028 #  define BOOST_SA_GCC_WORKAROUND
00029 #endif
00030 
00031 namespace boost{
00032 
00033 // HP aCC cannot deal with missing names for template value parameters
00034 template <bool x> struct STATIC_ASSERTION_FAILURE;
00035 
00036 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
00037 
00038 // HP aCC cannot deal with missing names for template value parameters
00039 template<int x> struct static_assert_test{};
00040 
00041 }
00042 
00043 //
00044 // Implicit instantiation requires that all member declarations be
00045 // instantiated, but that the definitions are *not* instantiated.
00046 //
00047 // It's not particularly clear how this applies to enum's or typedefs;
00048 // both are described as declarations [7.1.3] and [7.2] in the standard,
00049 // however some compilers use "delayed evaluation" of one or more of
00050 // these when implicitly instantiating templates.  We use typedef declarations
00051 // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
00052 // version gets better results from your compiler...
00053 //
00054 // Implementation:
00055 // Both of these versions rely on sizeof(incomplete_type) generating an error
00056 // message containing the name of the incomplete type.  We use
00057 // "STATIC_ASSERTION_FAILURE" as the type name here to generate
00058 // an eye catching error message.  The result of the sizeof expression is either
00059 // used as an enum initialiser, or as a template argument depending which version
00060 // is in use...
00061 // Note that the argument to the assert is explicitly cast to bool using old-
00062 // style casts: too many compilers currently have problems with static_cast
00063 // when used inside integral constant expressions.
00064 //
00065 #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS)
00066 
00067 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
00068 // __LINE__ macro broken when -ZI is used see Q199057
00069 // fortunately MSVC ignores duplicate typedef's.
00070 #define BOOST_STATIC_ASSERT( B ) \
00071    typedef ::boost::static_assert_test<\
00072       sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
00073       > boost_static_assert_typedef_
00074 #elif defined(BOOST_MSVC)
00075 #define BOOST_STATIC_ASSERT( B ) \
00076    typedef ::boost::static_assert_test<\
00077       sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
00078          BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
00079 #elif defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)
00080 // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error 
00081 // instead of warning in case of failure
00082 # define BOOST_STATIC_ASSERT( B ) \
00083     typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
00084         [ ::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >::value ]
00085 #elif defined(__sgi)
00086 // special version for SGI MIPSpro compiler
00087 #define BOOST_STATIC_ASSERT( B ) \
00088    BOOST_STATIC_CONSTANT(bool, \
00089      BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \
00090    typedef ::boost::static_assert_test<\
00091      sizeof(::boost::STATIC_ASSERTION_FAILURE< \
00092        BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\
00093          BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
00094 #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
00095 // special version for CodeWarrior <= 8.x
00096 #define BOOST_STATIC_ASSERT( B ) \
00097    BOOST_STATIC_CONSTANT(int, \
00098      BOOST_JOIN(boost_static_assert_test_, __LINE__) = \
00099        sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) )
00100 #else
00101 // generic version
00102 #define BOOST_STATIC_ASSERT( B ) \
00103    typedef ::boost::static_assert_test<\
00104       sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
00105          BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
00106 #endif
00107 
00108 #else
00109 // alternative enum based implementation:
00110 #define BOOST_STATIC_ASSERT( B ) \
00111    enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
00112       = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
00113 #endif
00114 
00115 
00116 #endif // BOOST_STATIC_ASSERT_HPP
00117 
00118 

L4vmm Reference Manual, written by Mario Schwalbe  © 2006-2008