Boost logo

Boost :

Subject: [boost] [MPL] Bug with default lambda expressions?
From: Louis Dionne (louis.dionne92_at_[hidden])
Date: 2013-05-21 16:43:58


Hi,

Some higher order algorithms in the MPL have a default for the lambda
expression they accept. A good example is `boost::mpl::equal`:

    template <typename S1, typename S2, typename Pred = is_same<_, _> >
    struct equal;

This works fine most of the time, but I was recently bitten by the following:

    template <typename VectorOfVectors, typename Vector>
    struct find_vector
        : find_if<
            VectorOfVectors, equal<Vector, _1>
>
    { };

    typedef find_vector<
        vector<
            vector<int, int>,
            vector<char, char>
>,
        vector<char, char>
>::type ThisWillBreak;

What happens here is that the `equal<Vector, _1>` expression inside
`find_vector` really is `equal<Vector, _1, is_same<_1, _2> >` because
of the default value for the predicate to `equal`. When the lambda is
evaluated, the placholders inside the inner `is_same<_1, _2>` expression
are replaced too, which yields unexpected results.

Using

    template <typename S1, typename S2,
                       typename Pred = typename lambda<is_same<_, _> >::type>
    struct equal;

or equivalently

    template <typename S1, typename S2, typename Pred = quote2<is_same> >
    struct equal;

fixes the issue. Also note that all of the unit tests of the MPL still pass
with these changes. Is the current behavior intended, or should I submit
a patch?

Regards,

Louis Dionne


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk