1 // Copyright (C) 2002 Brad King (brad.king@kitware.com)
2 //                    Douglas Gregor (gregod@cs.rpi.edu)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
8 // For more information, see http://www.boost.org
9 
10 
11 #include <boost/utility/addressof.hpp>
12 
13 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
14 #pragma warning(push, 3)
15 #endif
16 
17 #include <iostream>
18 
19 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
20 #pragma warning(pop)
21 #endif
22 
23 #include <boost/detail/lightweight_test.hpp>
24 
scalar_test(T * =0)25 template<class T> void scalar_test( T * = 0 )
26 {
27     T* px = new T();
28 
29     T& x = *px;
30     BOOST_TEST( boost::addressof(x) == px );
31 
32     const T& cx = *px;
33     const T* pcx = boost::addressof(cx);
34     BOOST_TEST( pcx == px );
35 
36     volatile T& vx = *px;
37     volatile T* pvx = boost::addressof(vx);
38     BOOST_TEST( pvx == px );
39 
40     const volatile T& cvx = *px;
41     const volatile T* pcvx = boost::addressof(cvx);
42     BOOST_TEST( pcvx == px );
43 
44     delete px;
45 }
46 
array_test(T * =0)47 template<class T> void array_test( T * = 0 )
48 {
49     T nrg[3] = {1,2,3};
50     T (*pnrg)[3] = &nrg;
51     BOOST_TEST( boost::addressof(nrg) == pnrg );
52 
53     T const cnrg[3] = {1,2,3};
54     T const (*pcnrg)[3] = &cnrg;
55     BOOST_TEST( boost::addressof(cnrg) == pcnrg );
56 }
57 
58 struct addressable
59 {
addressableaddressable60     addressable( int = 0 )
61     {
62     }
63 };
64 
65 struct useless_type {};
66 
67 class nonaddressable {
68 public:
69 
nonaddressable(int=0)70     nonaddressable( int = 0 )
71     {
72     }
73 
74     void dummy(); // Silence GCC warning: all member of class are private
75 
76 private:
77 
78     useless_type operator&() const;
79 };
80 
main()81 int main()
82 {
83     scalar_test<char>();
84     scalar_test<int>();
85     scalar_test<addressable>();
86     scalar_test<nonaddressable>();
87 
88     array_test<char>();
89     array_test<int>();
90     array_test<addressable>();
91     array_test<nonaddressable>();
92 
93     return boost::report_errors();
94 }
95