addressof_test.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // For more information, see http://www.boost.org
  8. #include <boost/utility/addressof.hpp>
  9. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  10. #pragma warning(push, 3)
  11. #endif
  12. #include <iostream>
  13. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  14. #pragma warning(pop)
  15. #endif
  16. #include <boost/detail/lightweight_test.hpp>
  17. template<class T> void scalar_test( T * = 0 )
  18. {
  19. T* px = new T();
  20. T& x = *px;
  21. BOOST_TEST( boost::addressof(x) == px );
  22. const T& cx = *px;
  23. const T* pcx = boost::addressof(cx);
  24. BOOST_TEST( pcx == px );
  25. volatile T& vx = *px;
  26. volatile T* pvx = boost::addressof(vx);
  27. BOOST_TEST( pvx == px );
  28. const volatile T& cvx = *px;
  29. const volatile T* pcvx = boost::addressof(cvx);
  30. BOOST_TEST( pcvx == px );
  31. delete px;
  32. }
  33. template<class T> void array_test( T * = 0 )
  34. {
  35. T nrg[3] = {1,2,3};
  36. T (*pnrg)[3] = &nrg;
  37. BOOST_TEST( boost::addressof(nrg) == pnrg );
  38. T const cnrg[3] = {1,2,3};
  39. T const (*pcnrg)[3] = &cnrg;
  40. BOOST_TEST( boost::addressof(cnrg) == pcnrg );
  41. }
  42. struct addressable
  43. {
  44. addressable( int = 0 )
  45. {
  46. }
  47. };
  48. struct useless_type {};
  49. class nonaddressable {
  50. public:
  51. nonaddressable( int = 0 )
  52. {
  53. }
  54. void dummy(); // Silence GCC warning: all member of class are private
  55. private:
  56. useless_type operator&() const;
  57. };
  58. int main()
  59. {
  60. scalar_test<char>();
  61. scalar_test<int>();
  62. scalar_test<addressable>();
  63. scalar_test<nonaddressable>();
  64. array_test<char>();
  65. array_test<int>();
  66. array_test<addressable>();
  67. array_test<nonaddressable>();
  68. return boost::report_errors();
  69. }