【问题标题】:Why std::bind cannot resolve function overloads with multiple arguments?为什么 std::bind 无法解析具有多个参数的函数重载?
【发布时间】:2015-11-08 08:12:56
【问题描述】:

考虑以下示例:

#include <iostream>
#include <functional>

using namespace std;

void f(int x,int y) {}
// void f(int x) {} // if this line is uncommented, code does not compile.

int main() {
    auto functor = std::bind(&f,1,placeholders::_1);
    cout << "works!" << endl;
    return 0;
}

它可以正常编译和运行 (https://ideone.com/fork/2SywE2),但取消注释注释行会导致编译器错误:

prog.cpp:在函数“int main()”中: prog.cpp:10:48: 错误: 没有匹配函数调用'bind(, int, const std::_Placeholder&)' 自动函子 = std::bind(&f,1,placeholders::_1); ^ 在 prog.cpp:2:0 包含的文件中: /usr/include/c++/5/functional:1462:5: 注意:候选:模板类型名 std::_Bind_helper::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ... ) 绑定(_Func&& __f, _BoundArgs&&... __args) ^ /usr/include/c++/5/functional:1462:5: 注意:模板参数推导/替换失败: prog.cpp:10:48:注意:无法推断模板参数“_Func” 自动函子 = std::bind(&f,1,placeholders::_1); ^ 在 prog.cpp:2:0 包含的文件中: /usr/include/c++/5/functional:1490:5:注意:候选:模板类型名 std::_Bindres_helper<_result _func _boundargs>::type std::bind(_Func&&, _BoundArgs&& ...) 绑定(_Func&& __f, _BoundArgs&&... __args) ^ /usr/include/c++/5/functional:1490:5:注意:模板参数推导/替换失败: prog.cpp:10:48:注意:无法推断模板参数“_Result” 自动函子 = std::bind(&f,1,placeholders::_1);

如果存在多个重载,为什么 std::bind 无法解析模板参数,因为重载具有不同数量的输入参数,并且调用 bind 意味着输入参数的数量为 2。

【问题讨论】:

    标签: templates c++11 lambda std-function


    【解决方案1】:

    当 C++ 编译器看到对std::bind(&amp;f,1,placeholders::_1) 的调用时,它不知道参数之间的任何关系。这些关系仅在模板被实例化时才可见。为了实例化它,编译器需要模板参数。但是&amp;f 是一个重载函数,因此它没有定义类型。因此,C++ 编译器无法实例化模板,因此即使在看到任何关系之前编译也会失败。

    您可以通过明确指定类型来解决此问题:

    std::bind(static_cast<void(*)(int,int)>(&f),1,placeholders::_1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 2019-04-20
      • 2013-11-19
      • 1970-01-01
      相关资源
      最近更新 更多