pointer_traits_pointer_to_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2017 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/core/pointer_traits.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. template<class T>
  10. class pointer {
  11. public:
  12. typedef typename boost::pointer_traits<T>::element_type element_type;
  13. pointer(T value)
  14. : value_(value) { }
  15. T get() const BOOST_NOEXCEPT {
  16. return value_;
  17. }
  18. static pointer<T> pointer_to(element_type& value) {
  19. return pointer<T>(&value);
  20. }
  21. private:
  22. T value_;
  23. };
  24. template<class T>
  25. inline bool
  26. operator==(const pointer<T>& lhs, const pointer<T>& rhs) BOOST_NOEXCEPT
  27. {
  28. return lhs.get() == rhs.get();
  29. }
  30. int main()
  31. {
  32. int i = 0;
  33. {
  34. typedef int* type;
  35. type p = &i;
  36. BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
  37. }
  38. {
  39. typedef pointer<int*> type;
  40. type p(&i);
  41. BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
  42. }
  43. {
  44. typedef pointer<pointer<int*> > type;
  45. type p(&i);
  46. BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
  47. }
  48. {
  49. typedef const int* type;
  50. type p = &i;
  51. BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
  52. }
  53. {
  54. typedef pointer<const int*> type;
  55. type p(&i);
  56. BOOST_TEST(boost::pointer_traits<type>::pointer_to(i) == p);
  57. }
  58. return boost::report_errors();
  59. }