#include <iostream>
#include <functional>
using callback = std::function<void(int, void*)>;
void AddCallback(callback cb) {}
void foo(int i) {}
int main() {
auto f = std::bind(&foo, std::placeholders::_1);
AddCallback(f);
}
我用 g++ 9.3.0 和 clang++ 10.0.0 尝试了代码,它们都编译结束没有错误。绑定(bind)结果的类型和回调的类型是否相同?一种是
std::function<void(int, void*)>
, 另一个等于 std::function<void(int)>
?为什么我可以调用AddCallback()
有不同的类型?
最佳答案
看来您可以将更多参数传递给 bind
的结果不必要的,它们将被默默地忽略。
If some of the arguments that are supplied in the call to [the result of
bind
] are not matched by any placeholders ..., the unused arguments are evaluated and discarded.
关于c++ - 为什么我可以使用错误的参数成功地 std::bind?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62711866/