Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:17

0001 /*=============================================================================
0002     Copyright (c) 2014 Paul Fultz II
0003     tap.h
0004     Distributed under the Boost Software License, Version 1.0. (See accompanying
0005     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 ==============================================================================*/
0007 
0008 #ifndef BOOST_HOF_GUARD_FUNCTION_TAP_H
0009 #define BOOST_HOF_GUARD_FUNCTION_TAP_H
0010 
0011 /// tap
0012 /// ===
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `tap` function invokes a function on the first argument passed in and
0018 /// then returns the first argument. This is useful in a chain of pipable
0019 /// function to perform operations on intermediate results. As a result, this
0020 /// function is [`pipable`](/include/boost/hof/pipable).
0021 /// 
0022 /// Synopsis
0023 /// --------
0024 /// 
0025 ///     template<class T, class F>
0026 ///     pipable constexpr T tap(T&& x, const F& f);
0027 /// 
0028 /// Requirements
0029 /// ------------
0030 /// 
0031 /// F must be:
0032 /// 
0033 /// * [UnaryInvocable](UnaryInvocable)
0034 /// 
0035 /// Example
0036 /// -------
0037 /// 
0038 ///     #include <boost/hof.hpp>
0039 ///     #include <cassert>
0040 ///     #include <iostream>
0041 ///     using namespace boost::hof;
0042 /// 
0043 ///     struct sum_f
0044 ///     {
0045 ///         template<class T, class U>
0046 ///         T operator()(T x, U y) const
0047 ///         {
0048 ///             return x+y;
0049 ///         }
0050 ///     };
0051 /// 
0052 ///     const pipable_adaptor<sum_f> sum = {};
0053 ///     int main() {
0054 ///         // Prints 3
0055 ///         int r = 1 | sum(2) | tap([](int i) { std::cout << i; }) | sum(2);
0056 ///         assert(r == 5);
0057 ///     }
0058 /// 
0059 
0060 #include <boost/hof/pipable.hpp>
0061 #include <boost/hof/apply.hpp>
0062 #include <boost/hof/detail/static_const_var.hpp>
0063 
0064 namespace boost { namespace hof { namespace detail {
0065 
0066 struct tap_f
0067 {
0068     template<class T, class F>
0069     constexpr T operator()(T&& x, const F& f) const
0070     BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT((boost::hof::apply(f, x), BOOST_HOF_FORWARD(T)(x)))
0071     {
0072         return boost::hof::apply(f, x), BOOST_HOF_FORWARD(T)(x);
0073     }
0074 };
0075 
0076 }
0077 
0078 BOOST_HOF_DECLARE_STATIC_VAR(tap, pipable_adaptor<detail::tap_f>);
0079 
0080 
0081 }} // namespace boost::hof
0082 
0083 #endif