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