Boost logo

Boost :

From: Neal D. Becker (nbecker_at_[hidden])
Date: 2002-10-15 10:31:45


I noticed the new ref.hpp. The description sounds like just what I
need. I want to pass a function object to std::generate by reference.

I made a small test, but it doesn't work. Am I doing something wrong,
or is ref.hpp not the solution to my problem?

--------------
#include <boost/ref.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

struct counter {
  int i;
  counter () : i (0) {}
  int operator()() { return i++; }
};

int main () {
  counter c;

  std::generate_n (ostream_iterator<int> (cout, " "), 10, boost::ref(c));
  cout << '\n';
  std::generate_n (ostream_iterator<int> (cout, " "), 10, boost::ref(c));
  cout << '\n';
}
------------------

g++ -g -o TestRef -I /usr/local/src/boost TestRef.cc
/usr/include/c++/3.2/bits/stl_algo.h: In function `_OutputIter
   std::generate_n(_OutputIter, _Size, _Generator) [with _OutputIter =
   std::ostream_iterator<int, char, std::char_traits<char> >, _Size = int,
   _Generator = boost::reference_wrapper<counter>]':
TestRef.cc:17: instantiated from here
/usr/include/c++/3.2/bits/stl_algo.h:952: no match for call to `(
   boost::reference_wrapper<counter>) ()'


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