I am reading a book, and there is below code :
mActionBinding[Fire].action = derivedAction<Aircraft>(
std::bind(&Aircraft::fire, _1));
the argument that is sent to derived action, will be sent two arguemnts later.
template<typename GameObject, typename Function>derived action function
std::function<void(SceneNode&, sf::Time)> derivedAction(Function fn)
{
return [=](SceneNode& node, sf::Time dt) expression
{
assert(dynamic_cast<GameObject *>(&node) != nullptr);
fn(static_cast<GameObject &>(node), dt);
};
}
the declaration of fire is this which is in Aircraft class:
class Aircraft:public SceneNode
{
public:
void fire()
};
I have read some articles about std::bind, but honestly I haven't seen anything like this.this is much more complex.
let me sort my questions:
1-based on my reading, this std::bind has three arguments! the member function, implicit class name and _1. is it true? does this mean, when we invoke it in derivedAction,instead of static_cast<gameObject &>(node)
, "this" will be sent to it?
2-fire has no argument at all!(except for implicit this), so why _1 give us no error? and how "dt" will be sent to it?
I am a little confused, references gives examples of simple uses, any other information about std::bind
, will be appreciated.
std::bind(&Aircraft::fire, _1)
1) There are two arguments passed to std::bind
. The first argument is &Aircraft::fire
, a pointer to the member function Aircraft::fire
. The second argument is std::placeholders::_1
, found by unqualified lookup due to the presence of a using namespace std::placeholders
somewhere.
fn(static_cast<GameObject &>(node), dt)
2) The argument static_cast<GameObject &>(node)
replaces the _1
placeholder. Aircraft::fire
has one parameter, the implicit this
, so static_cast<GameObject &>(node)
is supplied as the implicit this
argument to Aircraft::fire
. The argument dt
would replace the _2
placeholder, but since there is no _2
, it is ignored.