static_assert.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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)
  5. // See http://www.boost.org/libs/static_assert for documentation.
  6. /*
  7. Revision history:
  8. 02 August 2000
  9. Initial version.
  10. */
  11. #ifndef BOOST_STATIC_ASSERT_HPP
  12. #define BOOST_STATIC_ASSERT_HPP
  13. #include <boost/config.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #ifdef __BORLANDC__
  16. //
  17. // workaround for buggy integral-constant expression support:
  18. #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
  19. #endif
  20. #if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4))
  21. // gcc 3.3 and 3.4 don't produce good error messages with the default version:
  22. # define BOOST_SA_GCC_WORKAROUND
  23. #endif
  24. //
  25. // If the compiler issues warnings about old C style casts,
  26. // then enable this:
  27. //
  28. #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
  29. # define BOOST_STATIC_ASSERT_BOOL_CAST( x ) ((x) == 0 ? false : true)
  30. #else
  31. # define BOOST_STATIC_ASSERT_BOOL_CAST(x) (bool)(x)
  32. #endif
  33. #ifdef BOOST_HAS_STATIC_ASSERT
  34. # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B)
  35. #else
  36. namespace boost{
  37. // HP aCC cannot deal with missing names for template value parameters
  38. template <bool x> struct STATIC_ASSERTION_FAILURE;
  39. template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
  40. // HP aCC cannot deal with missing names for template value parameters
  41. template<int x> struct static_assert_test{};
  42. }
  43. //
  44. // Implicit instantiation requires that all member declarations be
  45. // instantiated, but that the definitions are *not* instantiated.
  46. //
  47. // It's not particularly clear how this applies to enum's or typedefs;
  48. // both are described as declarations [7.1.3] and [7.2] in the standard,
  49. // however some compilers use "delayed evaluation" of one or more of
  50. // these when implicitly instantiating templates. We use typedef declarations
  51. // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
  52. // version gets better results from your compiler...
  53. //
  54. // Implementation:
  55. // Both of these versions rely on sizeof(incomplete_type) generating an error
  56. // message containing the name of the incomplete type. We use
  57. // "STATIC_ASSERTION_FAILURE" as the type name here to generate
  58. // an eye catching error message. The result of the sizeof expression is either
  59. // used as an enum initialiser, or as a template argument depending which version
  60. // is in use...
  61. // Note that the argument to the assert is explicitly cast to bool using old-
  62. // style casts: too many compilers currently have problems with static_cast
  63. // when used inside integral constant expressions.
  64. //
  65. #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS)
  66. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  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_
  73. #elif defined(BOOST_MSVC)
  74. #define BOOST_STATIC_ASSERT( B ) \
  75. typedef ::boost::static_assert_test<\
  76. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST ( B ) >)>\
  77. BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
  78. #elif defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)
  79. // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
  80. // instead of warning in case of failure
  81. # define BOOST_STATIC_ASSERT( B ) \
  82. typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
  83. [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >::value ]
  84. #elif defined(__sgi)
  85. // special version for SGI MIPSpro compiler
  86. #define BOOST_STATIC_ASSERT( B ) \
  87. BOOST_STATIC_CONSTANT(bool, \
  88. BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \
  89. typedef ::boost::static_assert_test<\
  90. sizeof(::boost::STATIC_ASSERTION_FAILURE< \
  91. BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\
  92. BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
  93. #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
  94. // special version for CodeWarrior <= 8.x
  95. #define BOOST_STATIC_ASSERT( B ) \
  96. BOOST_STATIC_CONSTANT(int, \
  97. BOOST_JOIN(boost_static_assert_test_, __LINE__) = \
  98. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >) )
  99. #else
  100. // generic version
  101. #define BOOST_STATIC_ASSERT( B ) \
  102. typedef ::boost::static_assert_test<\
  103. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >)>\
  104. BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
  105. #endif
  106. #else
  107. // alternative enum based implementation:
  108. #define BOOST_STATIC_ASSERT( B ) \
  109. enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
  110. = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
  111. #endif
  112. #endif // ndef BOOST_HAS_STATIC_ASSERT
  113. #endif // BOOST_STATIC_ASSERT_HPP