]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | /* |
2 | Copyright 2017 Glen Joseph Fernandes | |
3 | (glenjofe@gmail.com) | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. | |
6 | (http://www.boost.org/LICENSE_1_0.txt) | |
7 | */ | |
8 | #include <boost/core/pointer_traits.hpp> | |
9 | #include <boost/core/lightweight_test.hpp> | |
10 | ||
11 | template<class T> | |
12 | class pointer { | |
13 | public: | |
14 | typedef typename boost::pointer_traits<T>::element_type element_type; | |
15 | pointer(T value) | |
16 | : value_(value) { } | |
17 | T operator->() const BOOST_NOEXCEPT { | |
18 | return value_; | |
19 | } | |
20 | private: | |
21 | T value_; | |
22 | }; | |
23 | ||
24 | template<class T> | |
25 | class special { | |
26 | public: | |
27 | special(T* value) | |
28 | : value_(value) { } | |
29 | T* get() const BOOST_NOEXCEPT { | |
30 | return value_; | |
31 | } | |
32 | private: | |
33 | T* value_; | |
34 | }; | |
35 | ||
36 | namespace boost { | |
37 | ||
38 | template<class T> | |
39 | struct pointer_traits<special<T> > { | |
40 | typedef special<T> pointer; | |
41 | typedef T element_type; | |
42 | typedef std::ptrdiff_t difference_type; | |
43 | template<class U> | |
44 | struct rebind_to { | |
45 | typedef special<U> type; | |
46 | }; | |
47 | #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) | |
48 | template<class U> | |
49 | using rebind = typename rebind_to<U>::type; | |
50 | #endif | |
51 | template<class U> | |
52 | static pointer pointer_to(U& v) BOOST_NOEXCEPT { | |
53 | return pointer(&v); | |
54 | } | |
55 | static element_type* to_address(const pointer& v) BOOST_NOEXCEPT { | |
56 | return v.get(); | |
57 | } | |
58 | }; | |
59 | ||
60 | } /* boost */ | |
61 | ||
62 | int main() | |
63 | { | |
64 | int i = 0; | |
65 | { | |
66 | typedef int* type; | |
67 | type p = &i; | |
68 | BOOST_TEST(boost::to_address(p) == &i); | |
69 | } | |
70 | { | |
71 | typedef pointer<int*> type; | |
72 | type p(&i); | |
73 | BOOST_TEST(boost::to_address(p) == &i); | |
74 | } | |
75 | { | |
76 | typedef pointer<pointer<int*> > type; | |
77 | type p(&i); | |
78 | BOOST_TEST(boost::to_address(p) == &i); | |
79 | } | |
80 | { | |
81 | typedef void* type; | |
82 | type p = &i; | |
83 | BOOST_TEST(boost::to_address(p) == &i); | |
84 | } | |
85 | { | |
86 | typedef pointer<void*> type; | |
87 | type p(&i); | |
88 | BOOST_TEST(boost::to_address(p) == &i); | |
89 | } | |
90 | { | |
91 | typedef const int* type; | |
92 | type p = &i; | |
93 | BOOST_TEST(boost::to_address(p) == &i); | |
94 | } | |
95 | { | |
96 | typedef pointer<const int*> type; | |
97 | type p(&i); | |
98 | BOOST_TEST(boost::to_address(p) == &i); | |
99 | } | |
100 | { | |
101 | typedef special<int> type; | |
102 | type p(&i); | |
103 | BOOST_TEST(boost::to_address(p) == &i); | |
104 | } | |
105 | { | |
106 | typedef special<void> type; | |
107 | type p(&i); | |
108 | BOOST_TEST(boost::to_address(p) == &i); | |
109 | } | |
110 | { | |
111 | typedef special<const int> type; | |
112 | type p(&i); | |
113 | BOOST_TEST(boost::to_address(p) == &i); | |
114 | } | |
115 | { | |
116 | typedef pointer<special<int> > type; | |
117 | type p(&i); | |
118 | BOOST_TEST(boost::to_address(p) == &i); | |
119 | } | |
120 | { | |
121 | typedef pointer<special<void> > type; | |
122 | type p(&i); | |
123 | BOOST_TEST(boost::to_address(p) == &i); | |
124 | } | |
125 | { | |
126 | typedef pointer<special<const int> > type; | |
127 | type p(&i); | |
128 | BOOST_TEST(boost::to_address(p) == &i); | |
129 | } | |
130 | return boost::report_errors(); | |
131 | } |