static_assert.qbk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. [library Boost.StaticAssert
  2. [copyright 2000 2005 Steve Cleary and John Maddock]
  3. [purpose Compile time diagnostics library]
  4. [license
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. <ulink url="http://www.boost.org/LICENSE_1_0.txt">
  8. http://www.boost.org/LICENSE_1_0.txt
  9. </ulink>)
  10. ]
  11. [authors [Maddock, John], [Cleary, Steve]]
  12. [category template]
  13. [category testing]
  14. [category generic]
  15. [last-revision $Date$]
  16. ]
  17. This manual is also available in
  18. [@http://sourceforge.net/projects/boost/files/boost-docs/
  19. printer friendly PDF format].
  20. [section:intro Overview and Tutorial]
  21. The header `<boost/static_assert.hpp>` supplies two macros:
  22. BOOST_STATIC_ASSERT(x)
  23. BOOST_STATIC_ASSERT_MSG(x, msg)
  24. Both generate a compile time error message if the integral-constant-expression `x`
  25. is not true. In other words, they are the compile time equivalent of the assert macro;
  26. this is sometimes known as a "compile-time-assertion", but will be called a
  27. "static assertion" throughout these docs. Note that if the condition is `true`,
  28. then the macros will generate neither code nor data - and the macros can also
  29. be used at either namespace, class or function scope. When used in a template,
  30. the static assertion will be evaluated at the time the template is instantiated;
  31. this is particularly useful for validating template parameters.
  32. If the C++0x `static_assert` feature is available, both macros will use it.
  33. For `BOOST_STATIC_ASSERT(x)`, the error message will be a stringized version of `x`.
  34. For `BOOST_STATIC_ASSERT_MSG(x, msg)`, the error message will be the `msg` string.
  35. If the C++0x `static_assert` feature is not available, `BOOST_STATIC_ASSERT_MSG(x, msg)`
  36. will be treated as `BOOST_STATIC_ASSERT(x)`.
  37. The material that follows assumes the C++0x `static_assert` feature is not available.
  38. One of the aims of `BOOST_STATIC_ASSERT` is to generate readable error messages.
  39. These immediately tell the user that a library is being used in a manner that
  40. is not supported. While error messages obviously differ from compiler to compiler,
  41. but you should see something like:
  42. Illegal use of STATIC_ASSERTION_FAILURE<false>
  43. Which is intended to at least catch the eye!
  44. You can use `BOOST_STATIC_ASSERT` at any place where you can place a declaration,
  45. that is at class, function or namespace scope, this is illustrated by the
  46. following examples:
  47. [section:namespace Use at namespace scope.]
  48. The macro can be used at namespace scope, if there is some requirement must
  49. always be true; generally this means some platform specific requirement.
  50. Suppose we require that `int` be at least a 32-bit integral type, and that `wchar_t`
  51. be an unsigned type. We can verify this at compile time as follows:
  52. #include <climits>
  53. #include <cwchar>
  54. #include <limits>
  55. #include <boost/static_assert.hpp>
  56. namespace my_conditions {
  57. BOOST_STATIC_ASSERT(std::numeric_limits<int>::digits >= 32);
  58. BOOST_STATIC_ASSERT(WCHAR_MIN >= 0);
  59. } // namespace my_conditions
  60. The use of the namespace my_conditions here requires some comment.
  61. The macro `BOOST_STATIC_ASSERT` works by generating an typedef declaration,
  62. and since the typedef must have a name, the macro generates one automatically by
  63. mangling a stub name with the value of `__LINE__`. When `BOOST_STATIC_ASSERT` is
  64. used at either class or function scope then each use of `BOOST_STATIC_ASSERT`
  65. is guaranteed to produce a name unique to that scope (provided you only use
  66. the macro once on each line). However when used in a header at namespace
  67. scope, that namespace can be continued over multiple headers, each of which
  68. may have their own static assertions, and on the "same" lines, thereby generating
  69. duplicate declarations. In theory the compiler should silently ignore duplicate
  70. typedef declarations, however many do not do so (and even if they do they are
  71. entitled to emit warnings in such cases). To avoid potential problems, if you
  72. use `BOOST_STATIC_ASSERT` in a header and at namespace scope, then enclose
  73. them in a namespace unique to that header.
  74. [endsect]
  75. [section:function Use at function scope]
  76. The macro is typically used at function scope inside template functions,
  77. when the template arguments need checking. Imagine that we have an
  78. iterator-based algorithm that requires random access iterators.
  79. If the algorithm is instantiated with iterators that do not meet our
  80. requirements then an error will be generated eventually, but this may
  81. be nested deep inside several templates, making it hard for the user to
  82. determine what went wrong. One option is to add a static assertion at
  83. the top level of the template, in that case if the condition is not met,
  84. then an error will be generated in a way that makes it reasonably obvious to
  85. the user that the template is being misused.
  86. #include <iterator>
  87. #include <boost/static_assert.hpp>
  88. #include <boost/type_traits.hpp>
  89. template <class RandomAccessIterator >
  90. RandomAccessIterator foo(RandomAccessIterator from,
  91. RandomAccessIterator to)
  92. {
  93. // this template can only be used with
  94. // random access iterators...
  95. typedef typename std::iterator_traits<
  96. RandomAccessIterator >::iterator_category cat;
  97. BOOST_STATIC_ASSERT(
  98. (boost::is_convertible<
  99. cat,
  100. const std::random_access_iterator_tag&>::value));
  101. //
  102. // detail goes here...
  103. return from;
  104. }
  105. A couple of footnotes are in order here: the extra set of parenthesis around the
  106. assert, is to prevent the comma inside the `is_convertible` template being
  107. interpreted by the preprocessor as a macro argument separator; the target type
  108. for `is_convertible` is a reference type, as some compilers have problems
  109. using `is_convertible` when the conversion is via a user defined constructor
  110. (in any case there is no guarantee that the iterator tag classes are
  111. copy-constructible).
  112. [endsect]
  113. [section:class Use at class scope]
  114. The macro is typically used inside classes that are templates.
  115. Suppose we have a template-class that requires an unsigned integral type with
  116. at least 16-bits of precision as a template argument, we can achieve this
  117. using something like this:
  118. #include <limits>
  119. #include <boost/static_assert.hpp>
  120. template <class UnsignedInt>
  121. class myclass
  122. {
  123. private:
  124. BOOST_STATIC_ASSERT_MSG(std::numeric_limits<UnsignedInt>::is_specialized, "myclass can only be specialized for types with numeric_limits support.");
  125. BOOST_STATIC_ASSERT_MSG(std::numeric_limits<UnsignedInt>::digits >= 16, "Template argument UnsignedInt must have at least 16 bits precision.")
  126. BOOST_STATIC_ASSERT_MSG(std::numeric_limits<UnsignedInt>::is_integer, "Template argument UnsignedInt must be an integer.");
  127. BOOST_STATIC_ASSERT_MSG(!std::numeric_limits<UnsignedInt>::is_signed, "Template argument UnsignedInt must not be signed.");
  128. public:
  129. /* details here */
  130. };
  131. [endsect]
  132. [section:templates Use in templates]
  133. Normally static assertions when used inside a class or function template,
  134. will not be instantiated until the template in which it is used is instantiated.
  135. However, there is one potential problem to watch out for: if the static assertion
  136. is not dependent upon one or more template parameters, then the compiler is
  137. permitted to evaluate the static assertion at the point it is first seen,
  138. irrespective of whether the template is ever instantiated, for example:
  139. template <class T>
  140. struct must_not_be_instantiated
  141. {
  142. BOOST_STATIC_ASSERT(false);
  143. };
  144. Will produce a compiler error with some compilers (for example Intel 8.1
  145. or gcc 3.4), regardless of whether the template is ever instantiated. A
  146. workaround in cases like this is to force the assertion to be dependent
  147. upon a template parameter:
  148. template <class T>
  149. struct must_not_be_instantiated
  150. {
  151. // this will be triggered if this type is instantiated
  152. BOOST_STATIC_ASSERT(sizeof(T) == 0);
  153. };
  154. [endsect]
  155. [endsect]
  156. [section:how How it works]
  157. `BOOST_STATIC_ASSERT` works as follows. There is class `STATIC_ASSERTION_FAILURE`
  158. which is defined as:
  159. namespace boost{
  160. template <bool> struct STATIC_ASSERTION_FAILURE;
  161. template <> struct STATIC_ASSERTION_FAILURE<true>{};
  162. }
  163. The key feature is that the error message triggered by the undefined
  164. expression `sizeof(STATIC_ASSERTION_FAILURE<0>)`, tends to be consistent
  165. across a wide variety of compilers. The rest of the machinery of
  166. `BOOST_STATIC_ASSERT` is just a way to feed the `sizeof` expression into a `typedef`.
  167. The use of a macro here is somewhat ugly; however boost members have spent
  168. considerable effort trying to invent a static assert that avoided macros,
  169. all to no avail. The general conclusion was that the good of a static assert
  170. working at namespace, function, and class scope outweighed the ugliness of a macro.
  171. [endsect]
  172. [section:test Test Programs]
  173. [table Test programs provided with static_assert
  174. [[Test Program][Expected to Compile][Description]]
  175. [[[@../../libs/static_assert/example/static_assert_example_1.cpp static_assert_example_1.cpp]] [Platform dependent.] [Namespace scope test program, may compile depending upon the platform. ]]
  176. [[[@../../libs/static_assert/example/static_assert_example_2.cpp static_assert_example_2.cpp]] [Yes] [Function scope test program. ]]
  177. [[[@../../libs/static_assert/example/static_assert_example_3.cpp static_assert_example_3.cpp]] [Yes] [Class scope test program. ]]
  178. [[[@../../libs/static_assert/test/static_assert_test.cpp static_assert_test.cpp]] [Yes] [Illustrates usage, and should always compile, really just tests compiler compatibility.]]
  179. [[[@../../libs/static_assert/test/static_assert_test_fail_1.cpp static_assert_test_fail_1.cpp]] [No] [Illustrates failure at namespace scope. ]]
  180. [[[@../../libs/static_assert/test/static_assert_test_fail_2.cpp static_assert_test_fail_2.cpp]] [No] [Illustrates failure at non-template function scope. ]]
  181. [[[@../../libs/static_assert/test/static_assert_test_fail_3.cpp static_assert_test_fail_3.cpp]] [No] [Illustrates failure at non-template class scope. ]]
  182. [[[@../../libs/static_assert/test/static_assert_test_fail_4.cpp static_assert_test_fail_4.cpp]] [No] [Illustrates failure at non-template class scope. ]]
  183. [[[@../../libs/static_assert/test/static_assert_test_fail_5.cpp static_assert_test_fail_5.cpp]] [No] [Illustrates failure at template class scope. ]]
  184. [[[@../../libs/static_assert/test/static_assert_test_fail_6.cpp static_assert_test_fail_6.cpp]] [No] [Illustrates failure at template class member function scope. ]]
  185. [[[@../../libs/static_assert/test/static_assert_test_fail_7.cpp static_assert_test_fail_7.cpp]] [No] [Illustrates failure of class scope example. ]]
  186. [[[@../../libs/static_assert/test/static_assert_test_fail_8.cpp static_assert_test_fail_8.cpp]] [No] [Illustrates failure of function scope example. ]]
  187. [[[@../../libs/static_assert/test/static_assert_test_fail_9.cpp static_assert_test_fail_9.cpp]] [No] [Illustrates failure of function scope example (part 2). ]]
  188. ]
  189. [endsect]