std :: bind和调用绑定函数的不同签名

时间:2012-03-23 23:16:41

标签: c++ bind visual-studio-2010

请解释,为什么在以下代码中没有错误:

void f()
{
}

void h()
{
    std::bind(f)(42);
}

为什么编译器在绑定函数f时不会抱怨std :: bind中的冗余参数?如果不应该,请解释为什么这有用。

1 个答案:

答案 0 :(得分:2)

此代码符合要求。传递给bind结果的参数仅在需要时使用。


使用标准中的术语:ustd::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