首页 > 解决方案 > 如何绑定 std::filesystem::copy?

问题描述

我想绑定std::filesystem::copy的第三个参数,即

void copy( const std::filesystem::path& from,
           const std::filesystem::path& to,
           std::filesystem::copy_options options );

到某个值,比如 std::filesystem::copy_options::none。

当我做:

namespace fs = std::filesystem;
auto f1 = std::bind( fs::copy, _1, _2, fs::copy_options::none );

gcc 编译器(10.3.0,c++20)给出错误(见下文):我做错了什么?伯特温

> error: no matching function for call to ‘bind(<unresolved overloaded
> function type>, const std::_Placeholder<1>&, const
> std::_Placeholder<2>&, std::filesystem::copy_options)’   641 |        
> auto f2 = std::bind( fs::copy, std::placeholders::_1,
> std::placeholders::_2, fs::copy_options::none );

标签: c++c++20

解决方案


std::filesystem::copy是一个重载函数。这意味着它的名称不能衰减为单一类型,因为我们不知道您想要哪个重载。您可以通过强制转换来解决这个问题,但您可以使用 lambda 表达式来创建包装器,而不是这样做

auto f1 = [](const auto& from, const auto& to) {
              fs::copy(from, to, fs::copy_options::none); 
          };

推荐阅读