Boost logo

Boost :

Subject: [boost] Fit: C++ function utility library
From: pfultz2 (pfultz2_at_[hidden])
Date: 2014-10-31 21:20:23


I developed a library called Fit, that provides a lot of utilites for
with function objects, here:

First, it provides a mechanism to construct function objects at compile time
using lambdas, for example:

    const constexpr auto sum = FIT_STATIC_LAMBDA(auto x, auto y)
        return x + y;

Secondly, it provides lots of adaptors to provide "enhancements" to the
function. For example, we could make the sum function pipable:

    const constexpr auto sum = pipable(FIT_STATIC_LAMBDA(auto x, auto y)
        return x + y;

Then call it like this:

    auto three = 1 | sum(2);

Or we could make it an infix operator:

    const constexpr auto sum = infix(FIT_STATIC_LAMBDA(auto x, auto y)
        return x + y;

And then use it like this:

    auto three = 1 <sum> 2;

Also, it provides the basic partial application and function composition:

    auto add_1 = partial(sum)(1);
    auto add_2 = compose(add_1, add_1);
    auto four = add_2(1);

Now, the greatest benifit is being to do better overloading and even
functions using lambdas. So, for example, if we wanted to write a more
sophisticated `print` function that iterated over ranges and fusion
recursively, and printed it all out, we can write this using the Fit

    // Some helpers for template constraints
    #define REQUIRES(...) typename std::enable_if<(decltype(__VA_ARGS__)()),
int>::type = 0
    template<template&lt;class...> class Trait, class... Ts>
    constexpr auto trait(Ts&&...)
        return std::integral_constant<bool,

    const constexpr auto print = fix(conditional(
        // Overload for strings
        FIT_STATIC_LAMBDA(auto, const std::string& x)
            std::cout << x << std::endl;
        // Overload for fusion sequences
        FIT_STATIC_LAMBDA(auto self, const auto& seq,
            boost::fusion::for_each(seq, self);
        // Overload for ranges
        FIT_STATIC_LAMBDA(auto self, const auto& range,
            for(const auto& x:range)
        // Overload for anything else
        FIT_STATIC_LAMBDA(auto, const auto& x)
            std::cout << x << std::endl;

The documentation for it can be found here:

Any feedback would be appreciated. Also, I don't know if there would be any
interest in incorporating this into boost.


View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at