hash_recursive_variant_test.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2016
  2. // Mikhail Maximov
  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. #include "boost/config.hpp"
  8. #include "boost/core/lightweight_test.hpp"
  9. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET)
  10. // Test is based on reported issues:
  11. // https://svn.boost.org/trac/boost/ticket/12508
  12. // https://svn.boost.org/trac/boost/ticket/12645
  13. // Following hash function was not found at compile time,
  14. // because boost::variant construction from boost::recursive_variant_
  15. // was forbidden.
  16. #include <unordered_set>
  17. #include "boost/variant.hpp"
  18. struct hash;
  19. using int_t = int;
  20. template <typename T>
  21. using basic_set_t = std::unordered_set<T, hash>;
  22. using value_t = boost::make_recursive_variant<
  23. int_t,
  24. basic_set_t<boost::recursive_variant_>
  25. >::type;
  26. using set_t = basic_set_t<value_t>;
  27. struct hash
  28. {
  29. size_t operator()(const value_t&) const
  30. {
  31. return 0;
  32. }
  33. };
  34. void run()
  35. {
  36. set_t s;
  37. int_t i = 3;
  38. value_t v = i;
  39. auto emplace_result = s.emplace(v); // raises error above
  40. BOOST_TEST(emplace_result.second);
  41. v = s;
  42. const set_t& check_set = boost::get<set_t>(v);
  43. BOOST_TEST(!check_set.empty());
  44. for (const auto& check_v : check_set) {
  45. BOOST_TEST(s.find(check_v) != s.end());
  46. }
  47. for (const auto& check_v : s) {
  48. BOOST_TEST(check_set.find(check_v) != check_set.end());
  49. }
  50. }
  51. #else // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET)
  52. // if no unordered_set and template aliases - does nothing
  53. void run() {}
  54. #endif
  55. int main()
  56. {
  57. run();
  58. return boost::report_errors();
  59. }