文件系统路径的提升范围副本的编译问题

Compilation issue with boost range copy of filesystem path

以下代码在用 1.46 编译时无法用 1.58 编译。我猜是类型转换的问题,但我做不对。

我的代码

#include <boost/filesystem.hpp>
#include <boost/range.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <vector>

using namespace boost;
using namespace filesystem;
using namespace adaptors;

int main(int argc, char ** argv)
{
    recursive_directory_iterator beg = recursive_directory_iterator(".");
    recursive_directory_iterator end = recursive_directory_iterator();

    iterator_range<recursive_directory_iterator> files = 
        make_iterator_range(beg, end);

    std::function<bool (path const & path)> fileFilter = 
        [](path const & path) -> bool {
        return is_regular_file(path);
    };

    std::vector<path> paths;
    copy(files | filtered(fileFilter), std::back_inserter(paths));

    // works:
    //copy(files, std::back_inserter(paths));

    // also works:
    //for(recursive_directory_iterator it = beg; it != end; ++it){
    //    if(fileFilter(*it)){
    //        paths.push_back(*it);
    //    }
    //}
}

错误信息(VS2010)

Error 1 error C2664: 'boost::filesystem::recursive_directory_iterator::recursive_directory_iterator(const boost::filesystem::path &,boost::filesystem::symlink_option::enum_type)' : cannot convert parameter 1 from 'boost::iterators::detail::postfix_increment_proxy' to 'const boost::filesystem::path &' [...]\boost.58.0_32\include\boost-1_58\boost\range\concepts.hpp 201 1 [...]

有人能帮忙吗?

编辑

归档为https://svn.boost.org/trac/boost/ticket/11228

这似乎是自 Boost 1.55 以来某处引入的错误。使用 boost 1.55 可以编译。

可能是这个错误:https://svn.boost.org/trac/boost/ticket/10989

boost_1_57_0/boost/concept_check.hpp|210 col 13| error: conversion from ‘boost::iterators::single_pass_traversal_tag’ to non-scalar type ‘boost::iterators::forward_traversal_tag’ requested

filtered 适配器要求输入为 forward_traversal 标签。但是 recursive_directory_iterator 只承诺 single_pass_traversal tag:

        BOOST_RANGE_CONCEPT_ASSERT((
            Convertible<
                BOOST_DEDUCED_TYPENAME ForwardIteratorConcept::traversal_category,
                forward_traversal_tag
            >));

解决方法

您现在可以禁用概念检查:-DBOOST_RANGE_ENABLE_CONCEPT_ASSERT=0:

Live On Coliru

#include <boost/filesystem.hpp>
#include <boost/range.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <vector>
#include <iostream>

namespace fs = boost::filesystem;
using namespace boost::adaptors;

int main() {
    fs::recursive_directory_iterator beg("."), end;

    auto fileFilter = [](fs::path const & path) { return is_regular_file(path); };

    std::vector<fs::path> paths;
    copy(boost::make_iterator_range(beg, end) | filtered(fileFilter), std::back_inserter(paths));

    for(auto& p : paths)
        std::cout << p << "\n";
}

版画

"./main.cpp"
"./a.out"