重载函数绑定的编译错误
本文描述了在绑定重载函数时出现的一个编译问题,并在解释其原因后给出了对应的解决办法。
◆ 问题
代码中存在重载(overloaded)的自由函数(也称全局函数)或类成员函数,当开发者尝试用 std::bind 绑定其中一个时,会提示如下编译错误:
error: no matching function for call to 'bind'
std::bind
^~~~~~~~~
note: candidate template ignored: couldn't infer template argument '_Fp'
bind(_Fp&& \__f, _BoundArgs&&... \__bound_args)
^
note: candidate template ignored: couldn't infer template argument '_Rp'
bind(_Fp&& \__f, _BoundArgs&&... \__bound_args)
^
◆ 示例
有两个重载的 shoot 自由函数(#1,#2),两个重载的类成员函数 Archer::shoot(#3,#4),
void shoot() { // #1
std::printf("\n\t[Free Function] Let an arrow fly... Hit!\n");
}
bool shoot(unsigned n) { // #2
std::printf("\n\t[Free Function] Let %d arrows fly... All missed!\n", n);
return false;
}
class Archer {
public:
void shoot() { // #3
std::printf("\n\t[Member Function] Let an arrow fly... Missed!\n");
}
bool shoot(unsigned n) { // #4
std::printf("\n\t[Member Function] Let %d arrows fly... All hit!\n", n);
return true;
}
};
开发者希望绑定一个自由函数(#2)或一个类成员函数(#4)时,
// bind #2
std::bind(shoot, 3)();
Archer hoyt;
// bind #4
std::bind(&Archer::shoot, &hoyt, 3)();
编译该代码时会抛出前述编译错误。
◆ 原因
重载函数的函数签名(signature)不同,是不同的函数。当开发者想绑定一个重载函数而仅给出名字时,编译器无法判定希望绑定的是哪一个函数,就会抛出编译错误。
◆ 解法
在绑定重载函数时,给出重载函数的签名。签名中的函数名用 (*) 或 (类名 :: *) 替代。
方法一,把函数签名作为类型参数传给 std::bind 函数,
// std::bind<Signature>(Function, Args...);
// example:
std::bind<bool(*)(unsigned)>(shoot, 3);
方法二,把函数签名作为类型参数传给 static_cast,再将转型后的结果传给 std::bind 函数,
// std::bind(static_cast<Signature>(Function), Args...);
// example:
std::bind(static_cast<bool(Archer::*)(unsigned)>(&Archer::shoot), &hoyt, 5);
◆ 最后
完整的代码请参考 [gitee] cnblogs/15572214 。
受限于作者的水平,读者如发现有任何错误或有疑问之处,请追加评论或发邮件联系 green-pi@qq.com。作者将在收到意见后的第一时间里予以回复。 本文来自博客园,作者:green-cnblogs,转载请注明原文链接:https://www.cnblogs.com/green-cnblogs/p/15572214.html 谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器