specialized_in_boost_and_other.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2008 Joseph Gauterin, Niels Dekker
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Tests whether instances of a class from a namespace other than boost are
  7. // properly swapped, when both boost and the other namespace have a custom
  8. // swap function for that class. Note that it shouldn't be necessary for a class
  9. // in an other namespace to have a custom swap function in boost, because the
  10. // boost::swap utility should find the swap function in the other namespace, by
  11. // argument dependent lookup (ADL). Unfortunately ADL isn't fully implemented
  12. // by some specific compiler versions, including Intel C++ 8.1, MSVC 7.1, and
  13. // Borland 5.9.3. Users of those compilers might consider adding a swap overload
  14. // to the boost namespace.
  15. #include <boost/utility/swap.hpp>
  16. #include <boost/core/lightweight_test.hpp>
  17. #define BOOST_CHECK BOOST_TEST
  18. #define BOOST_CHECK_EQUAL BOOST_TEST_EQ
  19. //Put test class in namespace other
  20. namespace other
  21. {
  22. #include "./swap_test_class.hpp"
  23. }
  24. //Provide swap function in namespace boost
  25. namespace boost
  26. {
  27. void swap(::other::swap_test_class& left, ::other::swap_test_class& right)
  28. {
  29. left.swap(right);
  30. }
  31. }
  32. //Provide swap function in namespace other
  33. namespace other
  34. {
  35. void swap(swap_test_class& left, swap_test_class& right)
  36. {
  37. left.swap(right);
  38. }
  39. }
  40. int main()
  41. {
  42. const other::swap_test_class initial_value1(1);
  43. const other::swap_test_class initial_value2(2);
  44. other::swap_test_class object1 = initial_value1;
  45. other::swap_test_class object2 = initial_value2;
  46. other::swap_test_class::reset();
  47. boost::swap(object1,object2);
  48. BOOST_CHECK(object1 == initial_value2);
  49. BOOST_CHECK(object2 == initial_value1);
  50. BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1);
  51. BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0);
  52. return boost::report_errors();
  53. }