decorate — Boost.HigherOrderFunctions 0.6 documentation

# decorate

## Header

    #include <boost/hof/decorate.hpp>

## Description

The `decorate` function adaptor helps create simple function decorators.

A function adaptor takes a function and returns a new functions whereas a decorator takes some parameters and returns a function adaptor. The `decorate` function adaptor will return a decorator that returns a function adaptor. Eventually, it will invoke the function with the user- provided parameter and function.

## Synopsis

    template<class F>
    constexpr decorate_adaptor<F> decorate(F f);

## Semantics

    assert(decorate(f)(x)(g)(xs...) == f(x, g, xs...));

## Requirements

F must be:

- ConstInvocable
- MoveConstructible

## Example

    #include <boost/hof.hpp>
    #include <cassert>
    #include <iostream>
    #include <string>
    using namespace boost::hof;

    struct logger_f
    {
        template<class F, class... Ts>
        auto operator()(const std::string& message, F&& f, Ts&&... xs) const
            -> decltype(f(std::forward<Ts>(xs)...))
        {
            // Message to print out when the function is called
            std::cout << message << std::endl;
            // Call the function
            return f(std::forward<Ts>(xs)...);
        }
    };
    // The logger decorator
    BOOST_HOF_STATIC_FUNCTION(logger) = boost::hof::decorate(logger_f());

    struct sum_f
    {
        template<class T, class U>
        T operator()(T x, U y) const
        {
            return x+y;
        }
    };

    BOOST_HOF_STATIC_FUNCTION(sum) = sum_f();
    int main() {
        // Use the logger decorator to print "Calling sum" when the function is called
        assert(3 == logger("Calling sum")(sum)(1, 2));
    }