More examples

As the Fit library is a collection of generic utilities related to functions, there is many useful cases with the library, but a key point of many of these utilities is that they can solve these problems with much simpler constructs than whats traditionally been done with metaprogramming. Lets take look at some of the use cases for using the Fit library.


The FIT_STATIC_FUNCTION will help initialize function objects at global scope, and will ensure that it is initialized at compile-time and (on platforms that support it) will have a unique address across translation units, thereby reducing executable bloat and potential ODR violations.

In addition, FIT_STATIC_LAMBDA allows initializing a lambda in the same manner. This allows for simple and compact function definitions when working with generic lambdas and function adaptors.

Of course, the library can still be used without requiring global function objects for those who prefer to avoid them will still find the library useful.


Instead of writing the projection multiple times in algorithms:

std::sort(std::begin(people), std::end(people),
          [](const Person& a, const Person& b) {
            return a.year_of_birth < b.year_of_birth;

We can use the by adaptor to project year_of_birth on the comparison operator:

std::sort(std::begin(people), std::end(people),
        by(&Person::year_of_birth, _ < _));

Ordering evaluation of arguments

When we write f(foo(), bar()), the standard does not guarantee the order in which the foo() and bar() arguments are evaluated. So with apply_eval we can order them from left-to-right:

apply_eval(f, [&]{ return foo(); }, [&]{ return bar(); });

Extension methods

Chaining many functions together, like what is done for range based libraries, can make things hard to read:

auto r = transform(
        [](int x) { return x > 2; }
    [](int x) { return x * x; }

It would be nice to write this:

auto r = numbers
    .filter([](int x) { return x > 2; })
    .transform([](int x) { return x * x; });

The proposal N4165 for Unified Call Syntax(UFCS) in C++17 would have allowed a function call of x.f(y) to become f(x, y). However, this was rejected by the comittee. So instead pipable functions can be used to achieve extension methods. So it can be written like this:

auto r = numbers
    | filter([](int x) { return x > 2; })
    | transform([](int x) { return x * x; });

Now, if some users feel a little worried about overloading the bitwise or operator, pipable functions can also be used with flow like this:

auto r = flow(
    filter([](int x) { return x > 2; }),
    transform([](int x) { return x * x; })

No fancy or confusing operating overloading and everything is still quite readable.