| 1 | /* |
| 2 | Copyright 2021 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> |
| 12 | struct valid { |
| 13 | typedef void type; |
| 14 | }; |
| 15 | |
| 16 | template<class, class = void> |
| 17 | struct has_pointer { |
| 18 | static const bool value = false; |
| 19 | }; |
| 20 | |
| 21 | template<class T> |
| 22 | struct has_pointer<T, typename valid<typename T::pointer>::type> { |
| 23 | static const bool value = true; |
| 24 | }; |
| 25 | |
| 26 | template<class, class = void> |
| 27 | struct has_element_type { |
| 28 | static const bool value = false; |
| 29 | }; |
| 30 | |
| 31 | template<class T> |
| 32 | struct has_element_type<T, typename valid<typename T::element_type>::type> { |
| 33 | static const bool value = true; |
| 34 | }; |
| 35 | |
| 36 | template<class, class = void> |
| 37 | struct has_difference_type { |
| 38 | static const bool value = false; |
| 39 | }; |
| 40 | |
| 41 | template<class T> |
| 42 | struct has_difference_type<T, |
| 43 | typename valid<typename T::difference_type>::type> { |
| 44 | static const bool value = true; |
| 45 | }; |
| 46 | |
| 47 | template<class, class, class = void> |
| 48 | struct has_rebind_to_type { |
| 49 | static const bool value = false; |
| 50 | }; |
| 51 | |
| 52 | template<class T, class U> |
| 53 | struct has_rebind_to_type<T, U, |
| 54 | typename valid<typename T::template rebind_to<U>::type>::type> { |
| 55 | static const bool value = true; |
| 56 | }; |
| 57 | |
| 58 | struct P1 { }; |
| 59 | |
| 60 | struct P2 { |
| 61 | typedef int element_type; |
| 62 | }; |
| 63 | |
| 64 | struct P3 { |
| 65 | typedef int element_type; |
| 66 | |
| 67 | #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) |
| 68 | template<class> |
| 69 | using rebind = P3; |
| 70 | #else |
| 71 | template<class> |
| 72 | struct rebind { |
| 73 | typedef P3 other; |
| 74 | }; |
| 75 | #endif |
| 76 | }; |
| 77 | |
| 78 | template<class T> |
| 79 | struct S { |
| 80 | typedef T element_type; |
| 81 | }; |
| 82 | |
| 83 | typedef S<int> P4; |
| 84 | |
| 85 | int main() |
| 86 | { |
| 87 | BOOST_TEST(!has_pointer<boost::pointer_traits<P1> >::value); |
| 88 | BOOST_TEST(!has_element_type<boost::pointer_traits<P1> >::value); |
| 89 | BOOST_TEST(!has_difference_type<boost::pointer_traits<P1> >::value); |
| 90 | BOOST_TEST((!has_rebind_to_type<boost::pointer_traits<P1>, char>::value)); |
| 91 | BOOST_TEST(has_pointer<boost::pointer_traits<P2> >::value); |
| 92 | BOOST_TEST(has_element_type<boost::pointer_traits<P2> >::value); |
| 93 | BOOST_TEST(has_difference_type<boost::pointer_traits<P2> >::value); |
| 94 | BOOST_TEST((!has_rebind_to_type<boost::pointer_traits<P2>, char>::value)); |
| 95 | BOOST_TEST(has_pointer<boost::pointer_traits<P3> >::value); |
| 96 | BOOST_TEST(has_element_type<boost::pointer_traits<P3> >::value); |
| 97 | BOOST_TEST(has_difference_type<boost::pointer_traits<P3> >::value); |
| 98 | BOOST_TEST((has_rebind_to_type<boost::pointer_traits<P3>, char>::value)); |
| 99 | BOOST_TEST(has_pointer<boost::pointer_traits<P4> >::value); |
| 100 | BOOST_TEST(has_element_type<boost::pointer_traits<P4> >::value); |
| 101 | BOOST_TEST(has_difference_type<boost::pointer_traits<P4> >::value); |
| 102 | BOOST_TEST((has_rebind_to_type<boost::pointer_traits<P4>, char>::value)); |
| 103 | BOOST_TEST(has_pointer<boost::pointer_traits<int*> >::value); |
| 104 | BOOST_TEST(has_element_type<boost::pointer_traits<int*> >::value); |
| 105 | BOOST_TEST(has_difference_type<boost::pointer_traits<int*> >::value); |
| 106 | BOOST_TEST((has_rebind_to_type<boost::pointer_traits<int*>, char>::value)); |
| 107 | return boost::report_errors(); |
| 108 | } |
| 109 | |