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 | |
---|
20 | // Function (block) scope |
---|
21 | void f() |
---|
22 | { |
---|
23 | BOOST_STATIC_ASSERT(sizeof(int) >= sizeof(short)); |
---|
24 | BOOST_STATIC_ASSERT(sizeof(char) == 1); |
---|
25 | } |
---|
26 | |
---|
27 | struct Bob |
---|
28 | { |
---|
29 | private: // can be in private, to avoid namespace pollution |
---|
30 | BOOST_STATIC_ASSERT(sizeof(int) >= sizeof(short)); |
---|
31 | BOOST_STATIC_ASSERT(sizeof(char) == 1); |
---|
32 | public: |
---|
33 | |
---|
34 | // Member function scope: provides access to member variables |
---|
35 | int x; |
---|
36 | char c; |
---|
37 | int f() |
---|
38 | { |
---|
39 | #ifndef _MSC_VER // broken sizeof in VC6 |
---|
40 | BOOST_STATIC_ASSERT(sizeof(x) >= sizeof(short)); |
---|
41 | BOOST_STATIC_ASSERT(sizeof(c) == 1); |
---|
42 | #endif |
---|
43 | return x; |
---|
44 | } |
---|
45 | }; |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | // Template class scope |
---|
50 | template <class Int, class Char> |
---|
51 | struct Bill |
---|
52 | { |
---|
53 | private: // can be in private, to avoid namespace pollution |
---|
54 | BOOST_STATIC_ASSERT(sizeof(Int) > sizeof(char)); |
---|
55 | public: |
---|
56 | |
---|
57 | // Template member function scope: provides access to member variables |
---|
58 | Int x; |
---|
59 | Char c; |
---|
60 | template <class Int2, class Char2> |
---|
61 | void f(Int2 , Char2 ) |
---|
62 | { |
---|
63 | BOOST_STATIC_ASSERT(sizeof(Int) == sizeof(Int2)); |
---|
64 | BOOST_STATIC_ASSERT(sizeof(Char) == sizeof(Char2)); |
---|
65 | } |
---|
66 | }; |
---|
67 | |
---|
68 | void test_Bill() // BOOST_CT_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 | |
---|
78 | int main() |
---|
79 | { |
---|
80 | test_Bill(); |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|