static_assert.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 ) static_assert( B, Msg )
  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)
  48. # else
  49. # define BOOST_STATIC_ASSERT_BOOL_CAST( x ) ((x) != 0)
  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. #ifndef BOOST_NO_CXX11_STATIC_ASSERT
  59. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  60. # define BOOST_STATIC_ASSERT( ... ) static_assert(__VA_ARGS__, #__VA_ARGS__)
  61. # else
  62. # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B)
  63. # endif
  64. #else
  65. namespace boost{
  66. // HP aCC cannot deal with missing names for template value parameters
  67. template <bool x> struct STATIC_ASSERTION_FAILURE;
  68. template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
  69. // HP aCC cannot deal with missing names for template value parameters
  70. template<int x> struct static_assert_test{};
  71. }
  72. //
  73. // Implicit instantiation requires that all member declarations be
  74. // instantiated, but that the definitions are *not* instantiated.
  75. //
  76. // It's not particularly clear how this applies to enum's or typedefs;
  77. // both are described as declarations [7.1.3] and [7.2] in the standard,
  78. // however some compilers use "delayed evaluation" of one or more of
  79. // these when implicitly instantiating templates. We use typedef declarations
  80. // by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
  81. // version gets better results from your compiler...
  82. //
  83. // Implementation:
  84. // Both of these versions rely on sizeof(incomplete_type) generating an error
  85. // message containing the name of the incomplete type. We use
  86. // "STATIC_ASSERTION_FAILURE" as the type name here to generate
  87. // an eye catching error message. The result of the sizeof expression is either
  88. // used as an enum initialiser, or as a template argument depending which version
  89. // is in use...
  90. // Note that the argument to the assert is explicitly cast to bool using old-
  91. // style casts: too many compilers currently have problems with static_cast
  92. // when used inside integral constant expressions.
  93. //
  94. #if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS)
  95. #if defined(BOOST_MSVC) && defined(BOOST_NO_CXX11_VARIADIC_MACROS)
  96. #define BOOST_STATIC_ASSERT( B ) \
  97. typedef ::boost::static_assert_test<\
  98. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST ( B ) >)>\
  99. BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
  100. #elif defined(BOOST_MSVC)
  101. #define BOOST_STATIC_ASSERT(...) \
  102. typedef ::boost::static_assert_test<\
  103. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST (__VA_ARGS__) >)>\
  104. BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
  105. #elif (defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)) && defined(BOOST_NO_CXX11_VARIADIC_MACROS)
  106. // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
  107. // instead of warning in case of failure
  108. # define BOOST_STATIC_ASSERT( B ) \
  109. typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
  110. [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >::value ]
  111. #elif (defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)) && !defined(BOOST_NO_CXX11_VARIADIC_MACROS)
  112. // agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
  113. // instead of warning in case of failure
  114. # define BOOST_STATIC_ASSERT(...) \
  115. typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
  116. [ ::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >::value ]
  117. #elif defined(__sgi)
  118. // special version for SGI MIPSpro compiler
  119. #define BOOST_STATIC_ASSERT( B ) \
  120. BOOST_STATIC_CONSTANT(bool, \
  121. BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \
  122. typedef ::boost::static_assert_test<\
  123. sizeof(::boost::STATIC_ASSERTION_FAILURE< \
  124. BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\
  125. BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
  126. #elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
  127. // special version for CodeWarrior <= 8.x
  128. #define BOOST_STATIC_ASSERT( B ) \
  129. BOOST_STATIC_CONSTANT(int, \
  130. BOOST_JOIN(boost_static_assert_test_, __LINE__) = \
  131. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >) )
  132. #else
  133. // generic version
  134. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  135. # define BOOST_STATIC_ASSERT( ... ) \
  136. typedef ::boost::static_assert_test<\
  137. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( __VA_ARGS__ ) >)>\
  138. BOOST_JOIN(boost_static_assert_typedef_, __LINE__) BOOST_ATTRIBUTE_UNUSED
  139. # else
  140. # define BOOST_STATIC_ASSERT( B ) \
  141. typedef ::boost::static_assert_test<\
  142. sizeof(::boost::STATIC_ASSERTION_FAILURE< BOOST_STATIC_ASSERT_BOOL_CAST( B ) >)>\
  143. BOOST_JOIN(boost_static_assert_typedef_, __LINE__) BOOST_ATTRIBUTE_UNUSED
  144. # endif
  145. #endif
  146. #else
  147. // alternative enum based implementation:
  148. # ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  149. # define BOOST_STATIC_ASSERT( ... ) \
  150. enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
  151. = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( __VA_ARGS__ ) >) }
  152. # else
  153. # define BOOST_STATIC_ASSERT(B) \
  154. enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
  155. = sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
  156. # endif
  157. #endif
  158. #endif // defined(BOOST_NO_CXX11_STATIC_ASSERT)
  159. #endif // BOOST_STATIC_ASSERT_HPP