addressof_test2.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) 2002 Brad King (brad.king@kitware.com)
  2. // Douglas Gregor (gregod@cs.rpi.edu)
  3. //
  4. // Copyright 2009 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // For more information, see http://www.boost.org
  10. #include <boost/utility/addressof.hpp>
  11. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  12. #pragma warning(push, 3)
  13. #endif
  14. #include <iostream>
  15. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  16. #pragma warning(pop)
  17. #endif
  18. #include <boost/detail/lightweight_test.hpp>
  19. template<class T> void scalar_test( T * = 0 )
  20. {
  21. T* px = new T();
  22. T& x = *px;
  23. BOOST_TEST( boost::addressof(x) == px );
  24. const T& cx = *px;
  25. const T* pcx = boost::addressof(cx);
  26. BOOST_TEST( pcx == px );
  27. volatile T& vx = *px;
  28. volatile T* pvx = boost::addressof(vx);
  29. BOOST_TEST( pvx == px );
  30. const volatile T& cvx = *px;
  31. const volatile T* pcvx = boost::addressof(cvx);
  32. BOOST_TEST( pcvx == px );
  33. delete px;
  34. }
  35. template<class T> void array_test( T * = 0 )
  36. {
  37. T nrg[3] = {1,2,3};
  38. T (*pnrg)[3] = &nrg;
  39. BOOST_TEST( boost::addressof(nrg) == pnrg );
  40. T const cnrg[3] = {1,2,3};
  41. T const (*pcnrg)[3] = &cnrg;
  42. BOOST_TEST( boost::addressof(cnrg) == pcnrg );
  43. }
  44. class convertible {
  45. public:
  46. convertible( int = 0 )
  47. {
  48. }
  49. template<class U> operator U () const
  50. {
  51. return U();
  52. }
  53. };
  54. class convertible2 {
  55. public:
  56. convertible2( int = 0 )
  57. {
  58. }
  59. operator convertible2* () const
  60. {
  61. return 0;
  62. }
  63. };
  64. int main()
  65. {
  66. scalar_test<convertible>();
  67. scalar_test<convertible2>();
  68. array_test<convertible>();
  69. array_test<convertible2>();
  70. return boost::report_errors();
  71. }