comparing.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*!
  2. @file
  3. Forward declares `boost::hana::comparing`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_COMPARING_HPP
  9. #define BOOST_HANA_FWD_COMPARING_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! Returns a function performing `equal` after applying a transformation
  13. //! to both arguments.
  14. //! @ingroup group-Comparable
  15. //!
  16. //! `comparing` creates an equivalence relation based on the result of
  17. //! applying a function to some objects, which is especially useful in
  18. //! conjunction with algorithms that accept a custom predicate that must
  19. //! represent an equivalence relation.
  20. //!
  21. //! Specifically, `comparing` is such that
  22. //! @code
  23. //! comparing(f) == equal ^on^ f
  24. //! @endcode
  25. //! or, equivalently,
  26. //! @code
  27. //! comparing(f)(x, y) == equal(f(x), f(y))
  28. //! @endcode
  29. //!
  30. //! @note
  31. //! This is not a tag-dispatched method (hence it can't be customized),
  32. //! but just a convenience function provided with the `Comparable` concept.
  33. //!
  34. //!
  35. //! Signature
  36. //! ---------
  37. //! Given a Logical `Bool` and a Comparable `B`, the signature is
  38. //! @f$ \mathtt{comparing} : (A \to B) \to (A \times A \to Bool) @f$.
  39. //!
  40. //!
  41. //! Example
  42. //! -------
  43. //! @include example/comparing.cpp
  44. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  45. constexpr auto comparing = [](auto&& f) {
  46. return [perfect-capture](auto&& x, auto&& y) {
  47. return equal(f(forwarded(x)), f(forwarded(y)));
  48. };
  49. };
  50. #else
  51. struct comparing_t {
  52. template <typename F>
  53. constexpr auto operator()(F&& f) const;
  54. };
  55. constexpr comparing_t comparing{};
  56. #endif
  57. BOOST_HANA_NAMESPACE_END
  58. #endif // !BOOST_HANA_FWD_COMPARING_HPP