static_assert.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #if defined(__GNUC__) && !defined(__GXX_EXPERIMENTAL_CXX0X__)
  16. //
  17. // This is horrible, but it seems to be the only we can shut up the
  18. // "anonymous variadic macros were introduced in C99 [-Wvariadic-macros]"
  19. // warning that get spewed out otherwise in non-C++11 mode.
  20. //
  21. #pragma GCC system_header
  22. #endif
  23. #ifndef BOOST_NO_CXX11_STATIC_ASSERT
  24. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  25. # define BOOST_STATIC_ASSERT_MSG( ... ) static_assert(__VA_ARGS__)
  26. # else
  27. # define BOOST_STATIC_ASSERT_MSG( B, Msg ) BOOST_STATIC_ASSERT( B )
  28. # endif
  29. #else
  30. # define BOOST_STATIC_ASSERT_MSG( B, Msg ) BOOST_STATIC_ASSERT( B )
  31. #endif
  32. #ifdef __BORLANDC__
  33. //
  34. // workaround for buggy integral-constant expression support:
  35. #define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
  36. #endif
  37. #if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4))
  38. // gcc 3.3 and 3.4 don't produce good error messages with the default version:
  39. # define BOOST_SA_GCC_WORKAROUND
  40. #endif
  41. //
  42. // If the compiler issues warnings about old C style casts,
  43. // then enable this:
  44. //
  45. #if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
  46. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  47. # define BOOST_STATIC_ASSERT_BOOL_CAST( ... ) ((__VA_ARGS__) == 0 ? false : true)
  48. # else
  49. # define BOOST_STATIC_ASSERT_BOOL_CAST( x ) ((x) == 0 ? false : true)
  50. # endif
  51. #else
  52. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  53. # define BOOST_STATIC_ASSERT_BOOL_CAST( ... ) (bool)(__VA_ARGS__)
  54. # else
  55. # define BOOST_STATIC_ASSERT_BOOL_CAST(x) (bool)(x)
  56. # endif
  57. #endif
  58. //
  59. // If the compiler warns about unused typedefs then enable this:
  60. //
  61. #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)))
  62. # define BOOST_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
  63. #else
  64. # define BOOST_STATIC_ASSERT_UNUSED_ATTRIBUTE
  65. #endif
  66. #ifndef BOOST_NO_CXX11_STATIC_ASSERT
  67. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  68. # define BOOST_STATIC_ASSERT( ... ) static_assert(__VA_ARGS__, #__VA_ARGS__)
  69. # else
  70. # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B)
  71. # endif
  72. #else
  73. namespace boost{
  74. // HP aCC cannot deal with missing names for template value parameters
  75. template <bool x> struct STATIC_ASSERTION_FAILURE;
  76. template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
  77. // HP aCC cannot deal with missing names for template value parameters
  78. template<int x> struct static_assert_test{};
  79. }
  80. //
  81. // Implicit instantiation requires that all member declarations be
  82. // instantiated, but that the definitions are *not* instantiated.
  83. //
  84. // It's not particularly clear how this applies to enum's or typedefs;
  85. // both are described as declarations [7.1.3] and [7.2] in the standard,
  86. // however some compilers use "delayed evaluation" of one or more of
  87. // these when implicitly instantiating templates. We use typedef declarations
  88. // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
  89. // version gets better results from your compiler...
  90. //
  91. // Implementation:
  92. // Both of these versions rely on sizeof(incomplete_type) generating an error
  93. // message containing the name of the incomplete type. We use
  94. // "STATIC_ASSERTION_FAILURE" as the type name here to generate
  95. // an eye catching error message. The result of the sizeof expression is either
  96. // used as an enum initialiser, or as a template argument depending which version
  97. // is in use...
  98. // Note that the argument to the assert is explicitly cast to bool using old-
  99. // style casts: too many compilers currently have problems with static_cast
  100. // when used inside integral constant expressions.
  101. //
  102. #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS)
  103. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  104. // __LINE__ macro broken when -ZI is used see Q199057
  105. // fortunately MSVC ignores duplicate typedef's.
  106. #define BOOST_STATIC_ASSERT( B ) \
  107. typedef ::boost::static_assert_test<\
  108. sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
  109. > boost_static_assert_typedef_
  110. #elif defined(BOOST_MSVC) && defined(BOOST_NO_CXX11_VARIADIC_MACROS)
  111. #define BOOST_STATIC_ASSERT( B ) \
  112. typedef ::boost::static_assert_test<\
  113. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST ( B ) >)>\
  114. BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
  115. #elif defined(BOOST_MSVC)
  116. #define BOOST_STATIC_ASSERT(...) \
  117. typedef ::boost::static_assert_test<\
  118. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST (__VA_ARGS__) >)>\
  119. BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
  120. #elif (defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)) && defined(BOOST_NO_CXX11_VARIADIC_MACROS)
  121. // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
  122. // instead of warning in case of failure
  123. # define BOOST_STATIC_ASSERT( B ) \
  124. typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
  125. [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >::value ]
  126. #elif (defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)) && !defined(BOOST_NO_CXX11_VARIADIC_MACROS)
  127. // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
  128. // instead of warning in case of failure
  129. # define BOOST_STATIC_ASSERT(...) \
  130. typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
  131. [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >::value ]
  132. #elif defined(__sgi)
  133. // special version for SGI MIPSpro compiler
  134. #define BOOST_STATIC_ASSERT( B ) \
  135. BOOST_STATIC_CONSTANT(bool, \
  136. BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \
  137. typedef ::boost::static_assert_test<\
  138. sizeof(::boost::STATIC_ASSERTION_FAILURE< \
  139. BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\
  140. BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
  141. #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
  142. // special version for CodeWarrior <= 8.x
  143. #define BOOST_STATIC_ASSERT( B ) \
  144. BOOST_STATIC_CONSTANT(int, \
  145. BOOST_JOIN(boost_static_assert_test_, __LINE__) = \
  146. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >) )
  147. #else
  148. // generic version
  149. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  150. # define BOOST_STATIC_ASSERT( ... ) \
  151. typedef ::boost::static_assert_test<\
  152. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >)>\
  153. BOOST_JOIN(boost_static_assert_typedef_, __LINE__) BOOST_STATIC_ASSERT_UNUSED_ATTRIBUTE
  154. # else
  155. # define BOOST_STATIC_ASSERT( B ) \
  156. typedef ::boost::static_assert_test<\
  157. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >)>\
  158. BOOST_JOIN(boost_static_assert_typedef_, __LINE__) BOOST_STATIC_ASSERT_UNUSED_ATTRIBUTE
  159. # endif
  160. #endif
  161. #else
  162. // alternative enum based implementation:
  163. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  164. # define BOOST_STATIC_ASSERT( ... ) \
  165. enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
  166. = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( __VA_ARGS__ ) >) }
  167. # else
  168. # define BOOST_STATIC_ASSERT(B) \
  169. enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
  170. = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
  171. # endif
  172. #endif
  173. #endif // defined(BOOST_NO_CXX11_STATIC_ASSERT)
  174. #endif // BOOST_STATIC_ASSERT_HPP