• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*=============================================================================
2    Copyright (c) 2014 Paul Fultz II
3    tap.h
4    Distributed under the Boost Software License, Version 1.0. (See accompanying
5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#ifndef BOOST_HOF_GUARD_FUNCTION_TAP_H
9#define BOOST_HOF_GUARD_FUNCTION_TAP_H
10
11/// tap
12/// ===
13///
14/// Description
15/// -----------
16///
17/// The `tap` function invokes a function on the first argument passed in and
18/// then returns the first argument. This is useful in a chain of pipable
19/// function to perform operations on intermediate results. As a result, this
20/// function is [`pipable`](/include/boost/hof/pipable).
21///
22/// Synopsis
23/// --------
24///
25///     template<class T, class F>
26///     pipable constexpr T tap(T&& x, const F& f);
27///
28/// Requirements
29/// ------------
30///
31/// F must be:
32///
33/// * [UnaryInvocable](UnaryInvocable)
34///
35/// Example
36/// -------
37///
38///     #include <boost/hof.hpp>
39///     #include <cassert>
40///     #include <iostream>
41///     using namespace boost::hof;
42///
43///     struct sum_f
44///     {
45///         template<class T, class U>
46///         T operator()(T x, U y) const
47///         {
48///             return x+y;
49///         }
50///     };
51///
52///     const pipable_adaptor<sum_f> sum = {};
53///     int main() {
54///         // Prints 3
55///         int r = 1 | sum(2) | tap([](int i) { std::cout << i; }) | sum(2);
56///         assert(r == 5);
57///     }
58///
59
60#include <boost/hof/pipable.hpp>
61#include <boost/hof/apply.hpp>
62#include <boost/hof/detail/static_const_var.hpp>
63
64namespace boost { namespace hof { namespace detail {
65
66struct tap_f
67{
68    template<class T, class F>
69    constexpr T operator()(T&& x, const F& f) const
70    BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT((boost::hof::apply(f, x), BOOST_HOF_FORWARD(T)(x)))
71    {
72        return boost::hof::apply(f, x), BOOST_HOF_FORWARD(T)(x);
73    }
74};
75
76}
77
78BOOST_HOF_DECLARE_STATIC_VAR(tap, pipable_adaptor<detail::tap_f>);
79
80
81}} // namespace boost::hof
82
83#endif
84