Skip to content

Latest commit

 

History

History
73 lines (53 loc) · 1.94 KB

File metadata and controls

73 lines (53 loc) · 1.94 KB

Overview

Description

This is a simple, but functional, C++14 lambda library. It takes advantage of the fact that the standard <functional> header already provides std::bind customization points (is_placeholder, is_bind_expression), and function objects such as std::plus, std::greater, std::logical_not, and std::bit_xor, corresponding to arithmetic, relational, logical and bitwise operators.

This allows the library to provide a minimal implementation that still lets expressions such as _1 + 5, _1 % 2 == 0, _1 > _2, or _1 == ' ' || _1 == '\t' to be composed and used as function objects.

For example, _1 + 5 is implemented as std::bind(std::plus<>, _1, 5).

These "lambda" expressions can also be freely combined with std::bind. For example, std::bind( f, _1 ) == std::bind( g, _1 ) and std::bind( f, _1 + _2 ) both work and have the expected behavior.

Usage Examples

Counting the Even Numbers

#include <boost/lambda2.hpp>
#include <algorithm>

using namespace boost::lambda2;

int count_even( int const * first, int const * last )
{
    return std::count_if( first, last, _1 % 2 == 0 );
}

Finding the First Whitespace Character

#include <boost/lambda2.hpp>
#include <algorithm>

char const * find_whitespace( char const * first, char const * last )
{
    using namespace boost::lambda2;

    return std::find_if( first, last,
        _1 == ' ' || _1 == '\t' || _1 == '\r' || _1 == '\n' );
}

Dependencies

None. A single, self-contained header.

Supported Compilers

  • GCC 5 or later with -std=c++14 or above

  • Clang 3.9 or later with -std=c++14 or above

  • Visual Studio 2015, 2017, 2019

Tested on Github Actions and Appveyor.