index_if.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*!
  2. @file
  3. Forward declares `boost::hana::index_if`.
  4. @copyright Louis Dionne 2013-2017
  5. @copyright Jason Rice 2017
  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_INDEX_IF_HPP
  10. #define BOOST_HANA_FWD_INDEX_IF_HPP
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/core/when.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Finds the value associated to the first key satisfying a predicate.
  15. //! @ingroup group-Iterable
  16. //!
  17. //! Given an `Iterable` structure `xs` and a predicate `pred`,
  18. //! `index_if(xs, pred)` returns a `hana::optional` containing an `IntegralConstant`
  19. //! of the index of the first element that satisfies the predicate or nothing
  20. //! if no element satisfies the predicate.
  21. //!
  22. //!
  23. //! @param xs
  24. //! The structure to be searched.
  25. //!
  26. //! @param predicate
  27. //! A function called as `predicate(x)`, where `x` is an element of the
  28. //! `Iterable` structure and returning whether `x` is the element being
  29. //! searched for. In the current version of the library, the predicate
  30. //! has to return an `IntegralConstant` holding a value that can be
  31. //! converted to `bool`.
  32. //!
  33. //!
  34. //! Example
  35. //! -------
  36. //! @include example/index_if.cpp
  37. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  38. constexpr auto index_if = [](auto&& xs, auto&& predicate) {
  39. return tag-dispatched;
  40. };
  41. #else
  42. template <typename S, typename = void>
  43. struct index_if_impl : index_if_impl<S, when<true>> { };
  44. struct index_if_t {
  45. template <typename Xs, typename Pred>
  46. constexpr auto operator()(Xs&& xs, Pred&& pred) const;
  47. };
  48. constexpr index_if_t index_if{};
  49. #endif
  50. BOOST_HANA_NAMESPACE_END
  51. #endif // !BOOST_HANA_FWD_INDEX_IF_HPP