proj — Boost.HigherOrderFunctions 0.6 documentation

# proj

## Header

    #include <boost/hof/proj.hpp>

## Description

The `proj` function adaptor applies a projection onto the parameters of another function. This is useful, for example, to define a function for sorting such that the ordering is based off of the value of one of its member fields.

Also, if just a projection is given, then the projection will be called for each of its arguments.

Note: All projections are always evaluated in order from left-to-right.

## Synopsis

    template<class Projection, class F>
    constexpr proj_adaptor<Projection, F> by(Projection p, F f);

    template<class Projection>
    constexpr proj_adaptor<Projection> by(Projection p);

## Semantics

    assert(by(p, f)(xs...) == f(p(xs)...));
    assert(by(p)(xs...) == p(xs)...);

## Requirements

Projection must be:

- UnaryInvocable
- MoveConstructible

F must be:

- ConstInvocable
- MoveConstructible

## Example

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

    struct foo
    {
        foo(int x_) : x(x_)
        {}
        int x;
    };

    int main() {
        assert(boost::hof::proj(&foo::x, _ + _)(foo(1), foo(2)) == 3);
    }

## References

- Projections
- Variadic print