Boost logo

Boost Users :

Subject: Re: [Boost-users] Using boost function input iterator with class member functions
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2011-09-05 01:38:03


Hi Michael,

On Sun, Sep 4, 2011 at 8:29 PM, Michael King <wmichaelking1_at_[hidden]> wrote:
> Can anyone suggest a more convenient solution to the problem I am trying to
> solve?

I was a little surprised that the function_input_iterator took its
argument by reference -- it seems like such arguments are typically
passed by value, allowing the client to use ref/cref. If you're able
to use boost range, I think you can get a much more attractive syntax
(example modified to work with g++ on windows). Using boost trunk:

#include<cstdlib>
#include<iostream>
#include<iterator>
#include<boost/bind.hpp>
#include<boost/range/adaptor/transformed.hpp>
#include<boost/range/algorithm/copy.hpp>
#include<boost/range/irange.hpp>

using boost::adaptors::transformed;

class foo {
  public:
    typedef double result_type;
    result_type return_random() const { return std::rand() /
(double)(RAND_MAX); }
};

int main(int argc, char** argv) {
  foo bar;
  boost::copy(
    boost::irange(1,10) | transformed(boost::bind(&foo::return_random, bar)),
    std::ostream_iterator<double>(std::cout, "\n"));
  return 0;
}

HTH,
Nate


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