请解释,为什么在以下代码中没有错误:
void f()
{
}
void h()
{
std::bind(f)(42);
}
为什么编译器在绑定函数f时不会抱怨std :: bind中的冗余参数?如果不应该,请解释为什么这有用。
答案 0 :(得分:2)
此代码符合要求。传递给bind结果的参数仅在需要时使用。
使用标准中的术语:u
是std::bind(f, t1, ..., tN)
的结果。
大约发言:
调用u(u1, u2, ..., uM)
时,f
被称为f(v1, ..., vN)
,其中vi
的值由以下算法确定:
//N is the N from `std::bind(f, t1, ..., tN)`
For each i in 1 to N:
if (ti is a reference wrapper) vi is the unwrapped version of t1
if (ti is a bind_expression) vi is the result of calling ti with u1, ..., uM
if (ti is a placeholder) vi is uj (where j is is_placeholder<decltype(ti)>::value)
otherwise vi is ti