tap.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. @file
  3. Forward declares `boost::hana::tap`.
  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_TAP_HPP
  9. #define BOOST_HANA_FWD_TAP_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Tap inside a monadic chain.
  14. //! @ingroup group-Monad
  15. //!
  16. //! Given a function `f`, `tap<M>` returns a new function which performs
  17. //! `f` on its argument and then returns the argument lifted in the `M`
  18. //! `Monad`. Combined with the property that `chain(m, lift<M>) == m`,
  19. //! this provides a way of executing an action inside a monadic chain
  20. //! without influencing its overall result. This is useful to e.g. insert
  21. //! debug statements or perform actions that are not tied to the chain but
  22. //! that need to be executed inside of it.
  23. //!
  24. //! @note
  25. //! Since C++ is not a pure language, it is possible to perform side
  26. //! effects inside the `f` function. Actually, side effects are the
  27. //! only reason why one might want to use `tap`. However, one should
  28. //! not rely on the side effects being done in any specific order.
  29. //!
  30. //!
  31. //! @tparam M
  32. //! The tag (a `Monad`) of the monads in the tapped monadic chain.
  33. //!
  34. //! @param f
  35. //! A function to be executed inside a monadic chain. It will be called
  36. //! as `f(x)`, where `x` is a value inside the previous monad in the
  37. //! chain. The result of `f` is always discarded.
  38. //!
  39. //!
  40. //! Example
  41. //! -------
  42. //! @include example/tap.cpp
  43. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  44. template <typename M>
  45. constexpr auto tap = [](auto&& f) {
  46. return tag-dispatched;
  47. };
  48. #else
  49. template <typename M, typename = void>
  50. struct tap_impl : tap_impl<M, when<true>> { };
  51. template <typename M>
  52. struct tap_t {
  53. template <typename F>
  54. constexpr auto operator()(F&& f) const;
  55. };
  56. template <typename M>
  57. constexpr tap_t<M> tap{};
  58. #endif
  59. BOOST_HANA_NAMESPACE_END
  60. #endif // !BOOST_HANA_FWD_TAP_HPP