hash.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. @file
  3. Forward declares `boost::hana::hash`.
  4. @copyright Louis Dionne 2016
  5. @copyright Jason Rice 2016
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  8. */
  9. #ifndef BOOST_HANA_FWD_HASH_HPP
  10. #define BOOST_HANA_FWD_HASH_HPP
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/core/when.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Returns a `hana::type` representing the compile-time hash of an object.
  15. //! @ingroup group-Hashable
  16. //!
  17. //! Given an arbitrary object `x`, `hana::hash` returns a `hana::type`
  18. //! representing the hash of `x`. In normal programming, hashes are
  19. //! usually numerical values that can be used e.g. as indices in an
  20. //! array as part of the implementation of a hash table. In the context
  21. //! of metaprogramming, we are interested in type-level hashes instead.
  22. //! Thus, `hana::hash` must return a `hana::type` object instead of an
  23. //! integer. This `hana::type` must somehow summarize the object being
  24. //! hashed, but that summary may of course lose some information.
  25. //!
  26. //! In order for the `hash` function to be defined properly, it must be
  27. //! the case that whenever `x` is equal to `y`, then `hash(x)` is equal
  28. //! to `hash(y)`. This ensures that `hana::hash` is a function in the
  29. //! mathematical sense of the term.
  30. //!
  31. //!
  32. //! Signature
  33. //! ---------
  34. //! Given a `Hashable` `H`, the signature is
  35. //! \f$
  36. //! \mathtt{hash} : H \to \mathtt{type\_tag}
  37. //! \f$
  38. //!
  39. //! @param x
  40. //! An object whose hash is to be computed.
  41. //!
  42. //!
  43. //! Example
  44. //! -------
  45. //! @include example/hash.cpp
  46. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  47. constexpr auto hash = [](auto const& x) {
  48. return tag-dispatched;
  49. };
  50. #else
  51. template <typename T, typename = void>
  52. struct hash_impl : hash_impl<T, when<true>> { };
  53. struct hash_t {
  54. template <typename X>
  55. constexpr auto operator()(X const& x) const;
  56. };
  57. constexpr hash_t hash{};
  58. #endif
  59. BOOST_HANA_NAMESPACE_END
  60. #endif // !BOOST_HANA_FWD_HASH_HPP