std::bind
(C++)
Introduced to me by Kajanan. We see this a lot in ROS2 codebase.
Resources
- https://cplusplus.com/reference/functional/bind/
- https://stackoverflow.com/questions/6610046/stdfunction-and-stdbind-what-are-they-and-when-should-they-be-used
#serendipity This is kind of like setting the capture clause in a Lambda Function.
In std::bind
, the number of the arguments is , when is the number of arguments to the function . i.e.
f
: This is the function (or callable) you are binding.arg1, arg2, ..., argN
: These are the arguments you are binding to the function. You can use actual values, or placeholders likestd::placeholders::_1
,std::placeholders::_2
, etc., which represent arguments to be provided when the new function is called.
std::bind
With Member functions
When you use std::bind
to bind a member functions, the first argument to std::bind
is the object (this
) on which the member function will be called. The placeholders (_1
, _2
, etc.) refer to the arguments that will be passed to the bound function when it is invoked.
Like this