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 fu
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 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
*unspecified* bind(F&& f, BoundArgs&&... bound_args);
p3 Returns: A forwarding call wrapper
g
with a weak result type (20.8.2). The effect ofg(u1, u2, ..., uM)
shall beINVOKE(fd, v1, v2, ..., vN, result_of
, where cv represents the cv-qualifiers of::type) g
and the values and types of the bound argumentsv1, v2, ..., vN
are determined as specified below.p10 The values of the bound arguments
v1, v2, ..., vN
and their corresponding typesV1, V2, ..., VN
depend on the typesTiD
derived from the call tobind
and the cv-qualifiers cv of the call wrapperg
as follows:
- if
TiD
isreference_wrapper
, the argument istid.get()
and its typeVi
isT&
;- if the value of
is_bind_expression
is::value true
, the argument istid(std::forward
and its type(uj)...) Vi
isresult_of
;::type - if the value
j
ofis_placeholder
is not zero, the argument is::value std::forward
and its type(uj) Vi
isUj&&
;- otherwise, the value is
tid
and its typeVi
isTiD cv &
.