hash.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/equal.hpp>
  6. #include <boost/hana/hash.hpp>
  7. #include <boost/hana/integral_constant.hpp>
  8. #include <boost/hana/type.hpp>
  9. #include <type_traits>
  10. #include <utility>
  11. namespace hana = boost::hana;
  12. // Sample implementation of a compile-time set data structure. Of course,
  13. // this naive implementation only works when no two elements of the same
  14. // set have the same hash.
  15. template <typename T>
  16. struct bucket { };
  17. template <typename ...T>
  18. struct set
  19. : bucket<typename decltype(hana::hash(std::declval<T>()))::type>...
  20. { };
  21. template <typename Set, typename T>
  22. struct contains
  23. : std::is_base_of<
  24. bucket<typename decltype(hana::hash(std::declval<T>()))::type>,
  25. Set
  26. >
  27. { };
  28. using Set = set<hana::int_<1>, hana::ulong<2>, hana::type<char>>;
  29. static_assert(contains<Set, hana::int_<1>>{}, "");
  30. static_assert(contains<Set, hana::ulong<2>>{}, "");
  31. static_assert(contains<Set, hana::type<char>>{}, "");
  32. static_assert(!contains<Set, hana::int_<3>>{}, "");
  33. static_assert(!contains<Set, hana::type<float>>{}, "");
  34. int main() { }