boost - 文件系统路径的 boost range 副本的编译问题

标签 boost boost-filesystem boost-range

以下代码在使用 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\1.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只 promise single_pass_traversal标签:

        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"

关于boost - 文件系统路径的 boost range 副本的编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823931/

相关文章:

C++ ReadDirectoryChangesW 和 Boost 将目录更改作为文件(旧名称)返回

c++ - boost 范围适配器

c++ - 为什么 ADL 不适用于 Boost.Range?

c++ - 多线程 C++ 消息传递

c++ - 从 boost::geometry::index::rtree 中删除点的问题

c++ - 如何划分boost::optional<double>?

c++ - Const std::filesystem::path 引用常量不受尊重,这是我做错的吗?

c++ - 使用 boost::filesystem 时出现链接器错误?

c++ - 使用两个访问者 boost 图形库