no_ambiguity_in_boost.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. // boost::swap internally does an unqualified function call to swap.
  7. // This could have led to ambiguity or infinite recursion, when the
  8. // objects to be swapped would themselves be from the boost namespace.
  9. // If so, boost::swap itself might be found by argument dependent lookup.
  10. // The implementation of boost::swap resolves this issue by giving
  11. // boost::swap two template argumetns, thereby making it less specialized
  12. // than std::swap.
  13. #include <boost/utility/swap.hpp>
  14. #include <boost/core/lightweight_test.hpp>
  15. #define BOOST_CHECK BOOST_TEST
  16. #define BOOST_CHECK_EQUAL BOOST_TEST_EQ
  17. //Put test class in namespace boost
  18. namespace boost
  19. {
  20. #include "./swap_test_class.hpp"
  21. }
  22. int main()
  23. {
  24. const boost::swap_test_class initial_value1(1);
  25. const boost::swap_test_class initial_value2(2);
  26. boost::swap_test_class object1 = initial_value1;
  27. boost::swap_test_class object2 = initial_value2;
  28. boost::swap_test_class::reset();
  29. boost::swap(object1,object2);
  30. BOOST_CHECK(object1 == initial_value2);
  31. BOOST_CHECK(object2 == initial_value1);
  32. BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),0);
  33. BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),3);
  34. return boost::report_errors();
  35. }