以下代码在使用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)
错误1错误C2664: &#39;的boost ::文件系统:: recursive_directory_iterator :: recursive_directory_iterator(常量 提高::文件系统::路径 &安培;,提振::文件系统:: symlink_option :: enum_type)&#39; :无法转换 参数1来自 &#39;的boost ::迭代器::详细:: postfix_increment_proxy&#39;至 &#39; const boost :: filesystem :: path &安培;&#39; [...] \ boost \ 1.58.0_32 \ include \ boost-1_58 \ boost \ range \ concepts.hpp 201 1 [...]
有人可以帮忙吗?
修改
答案 0 :(得分:2)
这似乎是自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 |错误:从'boost :: iterators :: single_pass_traversal_tag'转换为非标量类型'boost :: iterators :: forward_traversal_tag'
filtered
适配器要求输入为forward_traversal
标记。但recursive_directory_iterator
仅承诺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"