static_assert.hpp 7.1 KB

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