0
#include <boost/ref.hpp>
//#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/mem_fn.hpp>

using namespace std;
using namespace boost::lambda;

class Base {
  public:
  Base () {}
  bool toBeRemoved() const {
    return true;
  }
};

class status : public Base {
  std::string name_;
  bool ok_;
public:
  status(const std::string& name):name_(name),ok_(true) {}

  void break_it() {
    ok_=false;
  }

  bool is_broken() const {
    return ok_;
  }

  void report() const {
    std::cout << name_ << " is " <<
      (ok_ ? "working nominally":"terribly broken") << '\n';
  }
  std::string getStatus() const {
    return ok_ ? "1" : "0";
  }
};

class some_class {
 public:
 int test() {
   std::vector<boost::shared_ptr<status> > s_statuses = getStatus(); //some func 
   std::set<string> s;
   std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), boost::lambda::bind(boost::mem_fn(&status::getStatus), boost::ref(*_1)));

// approach #2
//   std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), boost::lambda::bind(boost::mem_fn(&status::getStatus), boost::ref(*_1), _1));

// approach #3
//   std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), boost::bind(&status::getStatus), _1));

   std::copy(s.begin(), s.end(), ostream_iterator<string>(std::cout, "-"));
   std::cout << endl;
   return 0;
 }
}

For all the approaches above, I am getting the error "can call member function without object" on the line containing the bind call. I have tried using boost::lambda::bind and boost::bind as well. Though this way of using bind works if objects are defined, for example in main function. I assume I am making some silly mistake here, but I am not able to figure out why these all approaches working, or it could be the case that this is not the right way of doing at all. Could someone please help me resolve this on how to properly use boost bind for non-static member of class which are stored in stl containers ?

Thanks,

DeltaVega
  • 75
  • 1
  • 8
  • 1
    Why not: `for (auto const & status : getStatus()) { s.insert(status.getStatus())' }` Also, how about not naming every function `getStatus`? – Kerrek SB Mar 31 '14 at 22:58
  • @KerrekSB: There is one function named `getStatus`. One. – Lightness Races in Orbit Mar 31 '14 at 23:03
  • @LightnessRacesinOrbit I see evidence of at least two competing `getStatus` functions. The... "thing" that returns `std::vector >` is also called that and is definitelty a different function. Amongst all the `s_statuses` this indeed seems more confusing than strictly necessary – sehe Mar 31 '14 at 23:34

1 Answers1

1

You should just need to use boost::mem_fn. (Note, you could also use std::mem_fn if available.)

std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), std::mem_fn(&status::getStatus));
zdan
  • 28,667
  • 7
  • 60
  • 71
  • For an upvote, explain that passing `*_1` as the first argument is not okay, since `*_1` is a `status` and cannot function as the `this` pointer. That's why the compiler thinks the OP is trying to call a non-static member function like a static member function. (Out of interest, I wonder whether `&*_1` is valid with a placeholder?) – Lightness Races in Orbit Apr 01 '14 at 00:16