ref_ct_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright David Abrahams and Aleksey Gurtovoy
  2. // 2002-2004. Distributed under the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // compile-time test for "boost/ref.hpp" header content
  6. // see 'ref_test.cpp' for run-time part
  7. #include <boost/ref.hpp>
  8. #include <boost/core/is_same.hpp>
  9. #include <boost/static_assert.hpp>
  10. #include <boost/detail/workaround.hpp>
  11. namespace {
  12. template< typename T, typename U >
  13. void ref_test(boost::reference_wrapper<U>)
  14. {
  15. typedef typename boost::reference_wrapper<U>::type type;
  16. BOOST_STATIC_ASSERT((boost::core::is_same<U,type>::value));
  17. BOOST_STATIC_ASSERT((boost::core::is_same<T,type>::value));
  18. }
  19. template< typename T >
  20. void assignable_test(T x)
  21. {
  22. x = x;
  23. }
  24. template< bool R, typename T >
  25. void is_reference_wrapper_test(T)
  26. {
  27. BOOST_STATIC_ASSERT(boost::is_reference_wrapper<T>::value == R);
  28. }
  29. template< typename R, typename Ref >
  30. void cxx_reference_test(Ref)
  31. {
  32. BOOST_STATIC_ASSERT((boost::core::is_same<R,Ref>::value));
  33. }
  34. template< typename R, typename Ref >
  35. void unwrap_reference_test(Ref)
  36. {
  37. typedef typename boost::unwrap_reference<Ref>::type type;
  38. BOOST_STATIC_ASSERT((boost::core::is_same<R,type>::value));
  39. }
  40. } // namespace
  41. int main()
  42. {
  43. int i = 0;
  44. int& ri = i;
  45. int const ci = 0;
  46. int const& rci = ci;
  47. // 'ref/cref' functions test
  48. ref_test<int>(boost::ref(i));
  49. ref_test<int>(boost::ref(ri));
  50. ref_test<int const>(boost::ref(ci));
  51. ref_test<int const>(boost::ref(rci));
  52. ref_test<int const>(boost::cref(i));
  53. ref_test<int const>(boost::cref(ri));
  54. ref_test<int const>(boost::cref(ci));
  55. ref_test<int const>(boost::cref(rci));
  56. // test 'assignable' requirement
  57. assignable_test(boost::ref(i));
  58. assignable_test(boost::ref(ri));
  59. assignable_test(boost::cref(i));
  60. assignable_test(boost::cref(ci));
  61. assignable_test(boost::cref(rci));
  62. // 'is_reference_wrapper' test
  63. is_reference_wrapper_test<true>(boost::ref(i));
  64. is_reference_wrapper_test<true>(boost::ref(ri));
  65. is_reference_wrapper_test<true>(boost::cref(i));
  66. is_reference_wrapper_test<true>(boost::cref(ci));
  67. is_reference_wrapper_test<true>(boost::cref(rci));
  68. is_reference_wrapper_test<false>(i);
  69. is_reference_wrapper_test<false, int&>(ri);
  70. is_reference_wrapper_test<false>(ci);
  71. is_reference_wrapper_test<false, int const&>(rci);
  72. // ordinary references/function template arguments deduction test
  73. cxx_reference_test<int>(i);
  74. cxx_reference_test<int>(ri);
  75. cxx_reference_test<int>(ci);
  76. cxx_reference_test<int>(rci);
  77. cxx_reference_test<int&, int&>(i);
  78. cxx_reference_test<int&, int&>(ri);
  79. cxx_reference_test<int const&, int const&>(i);
  80. cxx_reference_test<int const&, int const&>(ri);
  81. cxx_reference_test<int const&, int const&>(ci);
  82. cxx_reference_test<int const&, int const&>(rci);
  83. // 'unwrap_reference' test
  84. unwrap_reference_test<int>(boost::ref(i));
  85. unwrap_reference_test<int>(boost::ref(ri));
  86. unwrap_reference_test<int const>(boost::cref(i));
  87. unwrap_reference_test<int const>(boost::cref(ci));
  88. unwrap_reference_test<int const>(boost::cref(rci));
  89. unwrap_reference_test<int>(i);
  90. unwrap_reference_test<int>(ri);
  91. unwrap_reference_test<int>(ci);
  92. unwrap_reference_test<int>(rci);
  93. unwrap_reference_test<int&, int&>(i);
  94. unwrap_reference_test<int&, int&>(ri);
  95. unwrap_reference_test<int const&, int const&>(i);
  96. unwrap_reference_test<int const&, int const&>(ri);
  97. unwrap_reference_test<int const&, int const&>(ci);
  98. unwrap_reference_test<int const&, int const&>(rci);
  99. return 0;
  100. }