Opened 15 years ago

Closed 10 years ago

#1821 closed Bugs (fixed)

lambda and transform_iterator

Reported by: ka3a4ok <bismuth@…> Owned by: Dave Abrahams
Milestone: Boost 1.36.0 Component: iterator
Version: Boost 1.35.0 Severity: Problem
Keywords: Cc:

Description

This code doesn't compile.

#include <utility>
#include <list>
#include <boost\iterator\transform_iterator.hpp>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\bind.hpp>
#include <boost\function.hpp>

int main(int argc, char* argv[])
{
    using namespace std;
    using namespace boost;
    using namespace boost::lambda;
    
    typedef list<pair<int, double> > Tlist;

    make_transform_iterator
    (    Tlist().begin(),
         bind(&Tlist::value_type::first, _1)
    );

    return 0;
}

But this does:

#include <utility>
#include <list>
#include <boost\iterator\transform_iterator.hpp>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\bind.hpp>
#include <boost\function.hpp>

int main(int argc, char* argv[])
{
    using namespace std;
    using namespace boost;
    using namespace boost::lambda;
    
    typedef list<pair<int, double> > Tlist;

    make_transform_iterator
    (    Tlist().begin(),
         function<int(Tlist::value_type)>
         (  bind(&Tlist::value_type::first, _1)
         )
    );

    return 0;
}

Change History (5)

comment:1 by Dave Abrahams, 15 years ago

Resolution: invalid
Status: newclosed

The type of your bind expression does not satisfy the requirements of transform_iterator as described in http://boost.org/doc/libs/1_35_0/libs/iterator/doc/transform_iterator.html#transform-iterator-requirements

In particular, the expression result_of<UnaryFunction(iterator_traits<Iterator>::reference)>::type is not valid. This will all get better in C++0x when we have decltype, but in the meantime, it can't be considered a bug in Boost.Iterator.

comment:2 by anonymous, 10 years ago

now that we have decltype, should transform_iterator be adapated to take lambda expressions?

in reply to:  2 comment:3 by anonymous, 10 years ago

Supporting lambdas would be of great benefit. I was quite surprised to find they weren't supported.

comment:4 by Michel Morin, 10 years ago

Resolution: invalid
Status: closedreopened

comment:5 by Michel Morin, 10 years ago

Resolution: fixed
Status: reopenedclosed

Already fixed in Boost 1.47.

This ticket is a composite of

  • #864: Make lambda support result<>
  • #1427: transform_iterator should use boost/result_of to determine functor result type

which were fixed in Boost 1.44 and 1.47, respectively.

P.S. This ticket is about Boost.Lambda and NOT about C++11 lambdas. For C++11 lambdas, you should use decltype-based result_of. Please read result_of doc (especially about BOOST_RESULT_OF_USE_DECLTYPE macro).

Note: See TracTickets for help on using tickets.