at_key.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*!
  2. @file
  3. Forward declares `boost::hana::at_key`.
  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_AT_KEY_HPP
  9. #define BOOST_HANA_FWD_AT_KEY_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Returns the value associated to the given key in a structure, or fail.
  14. //! @ingroup group-Searchable
  15. //!
  16. //! Given a `key` and a `Searchable` structure, `at_key` returns the first
  17. //! value whose key is equal to the given `key`, and fails at compile-time
  18. //! if no such key exists. This requires the `key` to be compile-time
  19. //! `Comparable`, exactly like for `find`. `at_key` satisfies the following:
  20. //! @code
  21. //! at_key(xs, key) == find(xs, key).value()
  22. //! @endcode
  23. //!
  24. //! If the `Searchable` actually stores the elements it contains, `at_key`
  25. //! is required to return a lvalue reference, a lvalue reference to const
  26. //! or a rvalue reference to the found value, where the type of reference
  27. //! must match that of the structure passed to `at_key`. If the `Searchable`
  28. //! does not store the elements it contains (i.e. it generates them on
  29. //! demand), this requirement is dropped.
  30. //!
  31. //!
  32. //! @param xs
  33. //! The structure to be searched.
  34. //!
  35. //! @param key
  36. //! A key to be searched for in the structure. The key has to be
  37. //! `Comparable` with the other keys of the structure. In the current
  38. //! version of the library, the comparison of `key` with any other key
  39. //! of the structure must return a compile-time `Logical`.
  40. //!
  41. //!
  42. //! Example
  43. //! -------
  44. //! @include example/at_key.cpp
  45. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  46. constexpr auto at_key = [](auto&& xs, auto const& key) -> decltype(auto) {
  47. return tag-dispatched;
  48. };
  49. #else
  50. template <typename S, typename = void>
  51. struct at_key_impl : at_key_impl<S, when<true>> { };
  52. struct at_key_t {
  53. template <typename Xs, typename Key>
  54. constexpr decltype(auto) operator()(Xs&& xs, Key const& key) const;
  55. };
  56. constexpr at_key_t at_key{};
  57. #endif
  58. BOOST_HANA_NAMESPACE_END
  59. #endif // !BOOST_HANA_FWD_AT_KEY_HPP