static_assert_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // (C) Copyright Steve Cleary & 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 for most recent version including documentation.
  6. #include <boost/static_assert.hpp>
  7. //
  8. // all these tests should succeed.
  9. // some of these tests are rather simplistic (ie useless)
  10. // in order to ensure that they compile on all platforms.
  11. //
  12. // Namespace scope
  13. BOOST_STATIC_ASSERT(sizeof(int) >= sizeof(short));
  14. BOOST_STATIC_ASSERT(sizeof(char) == 1);
  15. BOOST_STATIC_ASSERT_MSG(sizeof(int) >= sizeof(short), "msg1");
  16. BOOST_STATIC_ASSERT_MSG(sizeof(char) == 1, "msg2");
  17. // Function (block) scope
  18. void f()
  19. {
  20. BOOST_STATIC_ASSERT(sizeof(int) >= sizeof(short));
  21. BOOST_STATIC_ASSERT(sizeof(char) == 1);
  22. BOOST_STATIC_ASSERT_MSG(sizeof(int) >= sizeof(short), "msg3");
  23. BOOST_STATIC_ASSERT_MSG(sizeof(char) == 1, "msg4");
  24. }
  25. struct Bob
  26. {
  27. private: // can be in private, to avoid namespace pollution
  28. BOOST_STATIC_ASSERT(sizeof(int) >= sizeof(short));
  29. BOOST_STATIC_ASSERT(sizeof(char) == 1);
  30. BOOST_STATIC_ASSERT_MSG(sizeof(int) >= sizeof(short), "msg5");
  31. BOOST_STATIC_ASSERT_MSG(sizeof(char) == 1, "msg6");
  32. public:
  33. // Member function scope: provides access to member variables
  34. int x;
  35. char c;
  36. int f()
  37. {
  38. #if defined(_MSC_VER) && _MSC_VER < 1300 // broken sizeof in VC6
  39. BOOST_STATIC_ASSERT(sizeof(x) >= sizeof(short));
  40. BOOST_STATIC_ASSERT(sizeof(c) == 1);
  41. BOOST_STATIC_ASSERT_MSG(sizeof(x) >= sizeof(short), "msg7");
  42. BOOST_STATIC_ASSERT_MSG(sizeof(c) == 1, "msg8");
  43. #endif
  44. return x;
  45. }
  46. };
  47. // Template class scope
  48. template <class Int, class Char>
  49. struct Bill
  50. {
  51. BOOST_STATIC_CONSTANT(int, value = 1);
  52. private: // can be in private, to avoid namespace pollution
  53. BOOST_STATIC_ASSERT(sizeof(Int) > sizeof(char));
  54. BOOST_STATIC_ASSERT_MSG(sizeof(Int) > sizeof(char), "msg9");
  55. public:
  56. // Template member function scope: provides access to member variables
  57. Int x;
  58. Char c;
  59. template <class Int2, class Char2>
  60. void f(Int2 , Char2 )
  61. {
  62. BOOST_STATIC_ASSERT(sizeof(Int) == sizeof(Int2));
  63. BOOST_STATIC_ASSERT(sizeof(Char) == sizeof(Char2));
  64. BOOST_STATIC_ASSERT_MSG(sizeof(Int) == sizeof(Int2), "msg10");
  65. BOOST_STATIC_ASSERT_MSG(sizeof(Char) == sizeof(Char2), "msg11");
  66. }
  67. };
  68. void test_Bill() // BOOST_STATIC_ASSERTs are not triggerred until instantiated
  69. {
  70. Bill<int, char> z;
  71. //Bill<int, int> bad; // will not compile
  72. int i = 3;
  73. char ch = 'a';
  74. z.f(i, ch);
  75. //z.f(i, i); // should not compile
  76. }
  77. int main()
  78. {
  79. test_Bill();
  80. //
  81. // Test variadic macro support:
  82. //
  83. #ifndef BOOST_NO_CXX11_VARIADIC_MACROS
  84. BOOST_STATIC_ASSERT(Bill<int, char>::value);
  85. #ifndef BOOST_NO_CXX11_STATIC_ASSERT
  86. BOOST_STATIC_ASSERT_MSG(Bill<int, char>::value, "This is a message");
  87. #endif
  88. #endif
  89. return 0;
  90. }