Hi,
I developed a library called Fit, that provides a lot of utilites for
dealing
with function objects, here:
https://github.com/pfultz2/Fit
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
recursive
functions using lambdas. So, for example, if we wanted to write a more
sophisticated `print` function that iterated over ranges and fusion
sequences
recursively, and printed it all out, we can write this using the Fit
library:
// Some helpers for template constraints
#define REQUIRES(...) typename std::enable_if<(decltype(__VA_ARGS__)()),
int>::type = 0
template class Trait, class... Ts>
constexpr auto trait(Ts&&...)
{
return std::integral_constant