问题
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.
回答1:
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
gwith a weak result type (20.8.2). The effect ofg(u1, u2, ..., uM)shall beINVOKE(fd, v1, v2, ..., vN, result_of<FD cv (V1, V2, ..., VN)>::type), where cv represents the cv-qualifiers ofgand the values and types of the bound argumentsv1, v2, ..., vNare determined as specified below.p10 The values of the bound arguments
v1, v2, ..., vNand their corresponding typesV1, V2, ..., VNdepend on the typesTiDderived from the call tobindand the cv-qualifiers cv of the call wrappergas follows:
- if
TiDisreference_wrapper<T>, the argument istid.get()and its typeViisT&;- if the value of
is_bind_expression<TiD>::valueistrue, the argument istid(std::forward<Uj>(uj)...)and its typeViisresult_of<TiD cv (Uj...)>::type;- if the value
jofis_placeholder<TiD>::valueis not zero, the argument isstd::forward<Uj>(uj)and its typeViisUj&&;- otherwise, the value is
tidand its typeViisTiD cv &.
回答2:
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.
来源:https://stackoverflow.com/questions/16164769/understanding-stdfunction-and-stdbind