Skip to content

[clang-tidy] Check request: boost-use-lambda2 #133583

@denzor200

Description

@denzor200

C++11 lambdas are known to be cumbersome. Even for a simple STL algorithm predicate its mandatory to specify:
-Three different braces;
-List of arguments;
-Return statement with the expression.
While specifying only the expression would be more than enough for STL algorithms. Thats the reason why some programmers refrain from STL algorithms usage and would prefer to write manual for loop. The Boost Lambda 2 library is intended to resolve the described problem.

Need a check that will find a C++11 lambdas which might be converted to lambdas from Boost Lambda 2 and will suggest to convert them.

BEFORE:

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

char const * find_whitespace( char const * first, char const * last )
{
    return std::find_if( first, last, [](char ch) {
        return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
    });
}

void print( std::map<int, std::string> const & m )
{
    std::for_each( m.begin(), m.end(), [](const auto& node) {
        std::cout << node->first << ": " << node->second << '\n';
    });
}

AFTER:

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

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' );
}

void print( std::map<int, std::string> const & m )
{
    using namespace boost::lambda2;
    std::for_each( m.begin(), m.end(),
        std::cout << _1->*first << ": " << _1->*second << '\n' );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions