How to use bind with explicit constructor conversion?

This doesn't compile. How best to accomplish this using generic STL or Boost algorithms/adapters? terry #include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <iterator> #include <cstdlib> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> using namespace std; using namespace boost; class MyInt { int _value; public: explicit MyInt(int x=0) : _value(x) { } int value() const { return _value; } }; // MyInt int main() { vector<MyInt> v; // Do this: // for (int i=0; i!=30; ++i) v.push_back(MyInt(rand())); generate_n(back_inserter(v), 30, bind(MyInt, bind(rand))); // Now print v. transform(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"), mem_fn(&MyInt::value)); return 0; } // main

Terry G wrote:
This doesn't compile. How best to accomplish this using generic STL or Boost algorithms/adapters?
IIRC, you'll need to use boost lambda facilities: http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.construction_... or boost::spirit::phoenix. Sorry I don't have a link for this one. Or use the techniques below:
terry
#include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <iterator> #include <cstdlib>
#include <boost/bind.hpp> #include <boost/mem_fn.hpp>
using namespace std; using namespace boost;
class MyInt { int _value; public: explicit MyInt(int x=0) : _value(x) { } int value() const { return _value; }
static Construct(int x=0){ return MyInt(x); }
}; // MyInt
int main() { vector<MyInt> v;
// Do this: // for (int i=0; i!=30; ++i) v.push_back(MyInt(rand())); generate_n(back_inserter(v), 30, bind(MyInt, bind(rand)));
generate_n(back_inserter(v), 30, bind(&MyInt::Construct, bind(rand)));
// Now print v. transform(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"), mem_fn(&MyInt::value));
return 0; } // main
Jeff Flinn
participants (2)
-
Jeff F
-
Terry G