Boost logo

Boost Users :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2005-09-27 11:03:42


  "Alexander Borghgraef" <alexander.borghgraef.rma_at_[hidden]> wrote in message news:9e8c52a20509270717488cef04_at_mail.gmail.com...
  On 9/27/05, Stuart Dootson <stuart.dootson_at_[hidden]> wrote:

   
    That's not going to work because the operator() call in the loop (i.e.
    m(...)) is evaluated when the lambda expression is created, not each
    time the lambda expression is evaluated.

    Try replacing

    var( m( c / m.w, c % m.w ) )

    with

    bind(&MyMatrix::operator(), var(m), var(c) / m->*&MyMatrix::w, var(c)
    % m->*&MyMatrix::w)

    This makes all accesses to m & c lazy (I think), so should update m as you want.

   Ok, that fixes it. Now, hardcore algorithmism aside, I don't see any advantage in this over the
  for loop: it's slower, and can hardly be called an improvement in readability. So I think I'll stick to
  the old-fashioned syntax in this case. Still, interesting to know that boost::lambda actually can do
  this.

Using std::copy and a function_output_iterator from the iterator adaptor library would IMHO, be more readable and resuable.

#include <boost/function_output_iterator.hpp>

struct matrix_filler
{
    matrix_filler( matrix& m ): m(m), counter( 0 ) {}

    void operator()( int val )const
    {
        m( counter / m.w , counter % m.w ) = val;

        ++counter
    }

    matrix& m;
    long counter;
};

std::copy( v.begin(), v.end(), boost::make_function_output_iterator(matrix_filler(m)) );

Or provide a "linear" iterator to your matrix class.

Jeff Flinn



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net