Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use std::bind to "attach" a state to a function?

Tags:

c++

std

c++11

I need to pass a std::function to some algorithm. The type of the function is

typedef std::function<bool(const double&)> Condition;

In the simplest case this function will look like this

bool simpleCondition(const double& d){return d<0.001;}

Now I want to pass the same condition but only if the condition is fullfilled a number of times in a row, the function should return true. I tried the following

class RepeatingCondition{
    public:
        static Condition getRepeatingCondition(Condition c,int reps){
            return std::bind(&RepeatingCondition::evalCondition,
                                   RepeatingCondition(c,reps),_1);
        }
    private:
        RepeatingCondition(Condition cc,int reps) : counter(0), 
                                              reps(reps),cond(cc){}
        bool evalCondition(const double& d){
            if (cond(d)){counter += 1;}
            else {counter = 0;}
            return (counter >= reps);
        }
        Condition cond;
        int counter,reps;
};

My compiler does not complain and it seems to work as expected. However, I dont really understand why (with a simple function pointer it would not work, right?). Also, I would like to know if there is a simpler way to achieve the same.

like image 603
463035818_is_not_a_number Avatar asked Dec 19 '14 17:12

463035818_is_not_a_number


People also ask

What does the function bind () do?

We use the Bind() method to call a function with the this value, this keyword refers to the same object which is currently selected . In other words, bind() method allows us to easily set which object will be bound by the this keyword when a function or method is invoked.

What is the difference between apply and bind?

Apply is very similar to the call function. The only difference is that in apply you can pass an array as an argument list. Bind is a function that helps you create another function that you can execute later with the new context of this that is provided.

Does Call apply bind work with Arrow function?

we cannot use call , apply , and bind , methods on Arrow functions to change the value of this , because arrow functions don't have their own this context, this inside arrow function will point to the outer/parent function in which it is present. So applying these methods on the arrow function will not make any effect.

Why we use call apply and bind?

The call, bind and apply methods can be used to set the this keyword independent of how a function is called. The bind method creates a copy of the function and sets the this keyword, while the call and apply methods sets the this keyword and calls the function immediately.


1 Answers

Can I use std::bind to “attach” a state to a function?

Yes, that's exactly what it's for. It returns an object (of an unspecified class type) containing the function pointer and all the bound arguments, with a function call operator() to invoke the function with those arguments.

std::function then encapsulates that object, allowing it to be passed around and invoked without knowing its type. This technique is known as type erasure.

with a simple function pointer it would not work, right?

Indeed, you need something more complicated for this to work. That's what std::bind and std::function provide.

Also, I would like to know if there is a simpler way to achieve the same.

A lambda is often more readable than a bind expression:

static Condition getRepeatingCondition(Condition c,int reps){
    RepeatingCondition rep(c,reps);
    return [rep](double d) mutable {return rep.evalCondition(d);};
}
like image 182
Mike Seymour Avatar answered Sep 27 '22 18:09

Mike Seymour