0

I want to replace some older code with simpler, functor based code. But I don't want to introduce a functor class for this and use boost::lambda/phoenix for this as I don't have C++11 at hand.

Old code looks like this

int player = ...;
Point middlePt = ...;
for(Point pt=<magic with nested loops>)
  if(this->IsMilitaryBuilding(pt) && (this->GetNode(pt).owner == player + 1))
    return true;
return false;

I have a function that calls Functor for every point (encapsulating the magic) and returns true when any of those calls returns true:

template<class Functor>
bool CheckPts(Point middlePt, Functor f);

Translating this for the first part of the if is easy:

return CheckPts(middlePt, bind(&IsMilitaryBuilding, this, _1));

And for the 2nd I'd want to do something like: bind(&GetNode, this, _1).owner == player+1 which is not supported.

What is the most readable way of doing this? I think this might be solvable by binding a reference to this and calling the functions directly using phoenix lambda but I did not found any references that go beyond simple 'Hello World' lambdas accessing only a simple member or a parameter.

Flamefire
  • 5,313
  • 3
  • 35
  • 70
  • [This](http://stackoverflow.com/questions/21861265/how-to-implement-a-lambda-function-for-a-sort-algorithm-involving-object-members) is not an exact duplicate, but it is quite similar. – llonesmiz May 28 '16 at 22:34
  • I think `` is highly relevant here. Also is `middlePt` int or `Point`? – sehe May 29 '16 at 21:39
  • `` is a 3-times nested loop that collects all points in a given radius. The most inner loop assigns the current `pt`. It is kinda complicated due to the given geometry, which is the reason I want to encapsulate it in a generic function as it essentially just loops over a set of points. For the sake of the question it could also be a vector of points. In the end, the loop is fixed and should be inside the function, but the loops body (the condition) should be supplied by the user of the function. – Flamefire May 30 '16 at 09:33

0 Answers0