Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding std::function and std::bind

I was playing arround with std::function and std::bind and I noticed something unintuitive and I would like to understand it better.

For example:

void fun()
{
}

void hun(std::string) 
{ 
}

int main()
{

   function<void(int)> g = &fun; //This fails as it should in my understanding.

   function<void(int)> f = std::bind(fun); //This works for reasons unknown to me     
   function<void(int, std::string)> h = std::bind(hun); //this doesn't work

return 0;
}

How is it possible to bind a function<void(int)> to a function that is void(). I could then call f(1) and get fun(). I would like to understand how this is done. Going inside Microsoft Visual Studio 2012's implementation of this got me lost in a sea of unreadable macros. so that is why I ask this question here.

like image 792
Alex Avatar asked Apr 23 '13 08:04

Alex


People also ask

What does std :: bind do in C++?

std::bind is for partial function application. That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b);

How does STD bind work?

Internally, std::bind() detects that a pointer to a member function is passed and most likely turns it into a callable objects, e.g., by use std::mem_fn() with its first argument.

What is the point of std :: function?

Instances of std::function can store, copy, and invoke any CopyConstructible Callable target -- functions (via pointers thereto), lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.

Does std :: function allocate memory?

std::function can store objects of arbitrary size, this means it must perform dynamic memory allocation in some cases. there are certain types for which std::function is guaranteed not to throw exceptions. This implies that there are certain types it must store without dynamic memory allocation.


2 Answers

If you don't use argument placeholders (_1, _2, ...), then any arguments passed to the function object returned from std::bind will just be discarded. With:

std::function<void(int)> f = std::bind(fun, std::placeholders::_1);

I get a (long and ugly) error as expected.

For the people interested in Standardese:

§20.8.9.1.2 [func.bind.bind]

template<class F, class... BoundArgs>
*unspecified* bind(F&& f, BoundArgs&&... bound_args);

p3 Returns: A forwarding call wrapper g with a weak result type (20.8.2). The effect of g(u1, u2, ..., uM) shall be INVOKE(fd, v1, v2, ..., vN, result_of<FD cv (V1, V2, ..., VN)>::type), where cv represents the cv-qualifiers of g and the values and types of the bound arguments v1, v2, ..., vN are determined as specified below.

p10 The values of the bound arguments v1, v2, ..., vN and their corresponding types V1, V2, ..., VN depend on the types TiD derived from the call to bind and the cv-qualifiers cv of the call wrapper g as follows:

  • if TiD is reference_wrapper<T>, the argument is tid.get() and its type Vi is T&;
  • if the value of is_bind_expression<TiD>::value is true, the argument is tid(std::forward<Uj>(uj)...) and its type Vi is result_of<TiD cv (Uj...)>::type;
  • if the value j of is_placeholder<TiD>::value is not zero, the argument is std::forward<Uj>(uj) and its type Vi is Uj&&;
  • otherwise, the value is tid and its type Vi is TiD cv &.
like image 90
Xeo Avatar answered Oct 21 '22 18:10

Xeo


The forwarding call wrapper generated by a call to function template bind can accept any number of extra parameters; these will be ignored. The effective arity and minimal signature of a bind expression is determined by the placeholders used in its construction, and which callable argument(s) they are bound to.

like image 36
ecatmur Avatar answered Oct 21 '22 18:10

ecatmur