Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the develop branch, built from commit dcea408971.

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 placeholders _1, _2, _3, and so on, for use with std::bind, 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.

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.5 or later with -std=c++14 or above

  • Visual Studio 2015, 2017, 2019

Tested on Travis and Appveyor.

Reference

<boost/lambda2/lambda2.hpp>

Synopsis

namespace boost {
namespace lambda2 {

template<class A, class B> auto operator+( A && a, B && b );
template<class A, class B> auto operator-( A && a, B && b );
template<class A, class B> auto operator*( A && a, B && b );
template<class A, class B> auto operator/( A && a, B && b );
template<class A, class B> auto operator%( A && a, B && b );
template<class A> auto operator-( A && a );

template<class A, class B> auto operator==( A && a, B && b );
template<class A, class B> auto operator!=( A && a, B && b );
template<class A, class B> auto operator>( A && a, B && b );
template<class A, class B> auto operator<( A && a, B && b );
template<class A, class B> auto operator>=( A && a, B && b );
template<class A, class B> auto operator<=( A && a, B && b );

template<class A, class B> auto operator&&( A && a, B && b );
template<class A, class B> auto operator||( A && a, B && b );
template<class A> auto operator!( A && a );

template<class A, class B> auto operator&( A && a, B && b );
template<class A, class B> auto operator|( A && a, B && b );
template<class A, class B> auto operator^( A && a, B && b );
template<class A> auto operator~( A && a );

} // namespace lambda2
} // namespace boost

Common Requirements

All operators defined in the subsequent sections only participate in overload resolution if at least one of their operands is such that for its unqualified type T, the expression std::is_placeholder<T>::value || std::is_bind_expression<T>::value is true.

Arithmetic Operators

template<class A, class B> auto operator+( A && a, B && b );
  • Returns:

    std::bind( std::plus<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator-( A && a, B && b );
  • Returns:

    std::bind( std::minus<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator*( A && a, B && b );
  • Returns:

    std::bind( std::multiplies<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator/( A && a, B && b );
  • Returns:

    std::bind( std::divides<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator%( A && a, B && b );
  • Returns:

    std::bind( std::modulus<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A> auto operator-( A && a );
  • Returns:

    std::bind( std::negate<>(), std::forward<A>(a) );

Relational Operators

template<class A, class B> auto operator==( A && a, B && b );
  • Returns:

    std::bind( std::equal_to<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator!=( A && a, B && b );
  • Returns:

    std::bind( std::not_equal_to<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator>( A && a, B && b );
  • Returns:

    std::bind( std::greater<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator<( A && a, B && b );
  • Returns:

    std::bind( std::less<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator>=( A && a, B && b );
  • Returns:

    std::bind( std::greater_equal<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator<=( A && a, B && b );
  • Returns:

    std::bind( std::less_equal<>(), std::forward<A>(a), std::forward<B>(b) );

Logical Operators

template<class A, class B> auto operator&&( A && a, B && b );
  • Returns:

    std::bind( std::logical_and<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator||( A && a, B && b );
  • Returns:

    std::bind( std::logical_or<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A> auto operator!( A && a );
  • Returns:

    std::bind( std::logical_not<>(), std::forward<A>(a) );

Bitwise Operators

template<class A, class B> auto operator&( A && a, B && b );
  • Returns:

    std::bind( std::bit_and<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator|( A && a, B && b );
  • Returns:

    std::bind( std::bit_or<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A, class B> auto operator^( A && a, B && b );
  • Returns:

    std::bind( std::bit_xor<>(), std::forward<A>(a), std::forward<B>(b) );

template<class A> auto operator~( A && a );
  • Returns:

    std::bind( std::bit_not<>(), std::forward<A>(a) );

This documentation is copyright 2020 Peter Dimov and is distributed under the Boost Software License, Version 1.0.