如何正确绑定std::filesystem::copy函数并固定其第三个参数
错误原因
std::filesystem::copy存在多个重载版本,你直接将fs::copy传递给std::bind时,编译器无法自动推导你需要绑定的是哪一个重载签名,因此会抛出「未解析的重载函数类型」的编译错误。
解决方案
有两种常用的修复方式:
方案1:显式指定重载类型
通过强制类型转换明确告诉编译器你要选择的重载版本:
namespace fs = std::filesystem; // 定义和你需要的重载匹配的函数指针类型 using CopyFuncType = void(*)(const fs::path&, const fs::path&, fs::copy_options); auto f1 = std::bind( static_cast<CopyFuncType>(fs::copy), std::placeholders::_1, std::placeholders::_2, fs::copy_options::none );
方案2:用Lambda替代std::bind(更推荐)
Lambda不存在重载解析的问题,代码可读性和执行效率都更优:
namespace fs = std::filesystem; auto f1 = [](const fs::path& from, const fs::path& to) { fs::copy(from, to, fs::copy_options::none); };
内容的提问来源于stack exchange,提问作者Bertwim van Beest
相关产品推荐
相关产品推荐

