1

Is it possible to access a static member function through std::for_each?

I've hit a problem with a class I'm trying to code. I have a class Foo which in the member section initialises an object of Boo and I need to access this inside of a static member function that is used in std::foreach() details below:

Foo.h

class Foo {
   public: 
      Foo() {

          w = getInstanceOfAnotherClass(0, 0); // this works fine!
      }
      void Transform();

      static inline void processBlock(std::vector<double> &vect);

   private: 
     std::vector<std::vector<double> > data;
     Boo* w;
}; 

Here is the problem: Inside of the member function Transform I have the following:

void Foo::Transform() 
{
    std::for_each(data.begin(), data.end(), processBlock);
}

And in ProcessBlock I have the following:

void Foo::processBlock(std::vector<double> &vect) 
{
    std::vector<double> vars = w.getDataBack<double>(); 
}

The error that is returned is that w invalid use of member 'w' in static member function, now, I know what the problem is.. But I don't know know of a workaround. I decided to create another function that wasn't static and then call this function from inside of processBlock, however, the member function cannot be called without declaring an object, which, would therefore re-set the value of w and this is not what I want.

I hope someone can help and this post isn't confusing.

Phorce
  • 2,632
  • 13
  • 43
  • 76
  • 3
    Can you use C++11? If so, the easiest solution is to write a lambda that captures `this` instead of a static function. – jrok Aug 16 '13 at 19:29
  • @jrok Hey thank you for the reply. I can use C++11 but, I'm new to the new standards and Lambda, could you recommend a place to research this, or, provide an example? Thank you – Phorce Aug 16 '13 at 19:30
  • since `w` is a pointer, you also should write `w->` – TemplateRex Aug 16 '13 at 19:30
  • 1
    I think a very similar question was asked just today. Let me try to find it. EDIT: [here](http://stackoverflow.com/questions/18273997/passing-a-private-method-of-the-class-as-the-compare-operator-for-stdsort/18274208#18274208). – jrok Aug 16 '13 at 19:30
  • Couldn't you call 'processBlock' with a pointer to `this` as a parameter? – Joe M Aug 16 '13 at 19:33
  • @Phorce: The lambda would look like `[this](std::vector &vect){processBlock(vect);}` If you wait a year, you should be able to change the parameter type to `auto&` when C++14 introduces polymorphic lambdas. – Mike Seymour Aug 16 '13 at 21:20
  • 1
    @JoeMajsterski: No, because `for_each` calls its function with a single argument. If you added a second parameter to the static function, you'd need a lambda or `bind` just as you would with a non-static function. – Mike Seymour Aug 16 '13 at 21:23

1 Answers1

1

FooThe way I'd do it is with a function call instead of a static function. Use

void processBlock(std::vector<double> &vect);

And

std::for_each(data.begin(),data.end(),std::bind(&Foo::processBlock,this,_1))

IdeaHat
  • 7,641
  • 1
  • 22
  • 53
  • This works :) I really want to try to Lambda approach tho, but, this works great! – Phorce Aug 16 '13 at 19:36
  • Lamda's work fine, I'm just more used to bind because visual studio 10 :-P. It should just be `[this](std::vector& vect){this->processBlock(vect);}` but I'm honestly not all that familiar with it, and I can't test it now and don't like posting things I can't test. – IdeaHat Aug 16 '13 at 19:40