这是我使用过滤迭代器时遇到的问题的精简版本(因此要求我以不同的方式重写它以避免过滤器是没有意义的)。奇怪的是,在真正的代码中is_sorted
似乎只有问题,其他用途似乎工作正常。
#include <vector>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm_ext/is_sorted.hpp>
int main(int argc, const char* argv[])
{
using namespace boost::adaptors;
std::vector<int> all = {1,2,3,4,5,6,7,8,9};
auto some = all | filtered([] (int i) { return i % 2; });
return boost::is_sorted(some);
}
这无法使用 Clang++ 3.5 和 G++ 4.9 编译(在 Mac OS X 上,最新):
$ clang++-mp-3.5 -std=c++11 -isystem /opt/local/include/ foo.cc
In file included from foo.cc:3:
In file included from /opt/local/include/boost/range/algorithm_ext/is_sorted.hpp:18:
/opt/local/include/boost/detail/is_sorted.hpp:25:28: error: object of type
'boost::filter_iterator<(lambda at foo.cc:9:30), std::__1::__wrap_iter<int
*> >' cannot be assigned because its copy assignment operator is
implicitly deleted
for (; it != last; first = it, ++it)
^
...
/opt/local/include/boost/iterator/filter_iterator.hpp:106:17: note: copy
assignment operator of 'filter_iterator<(lambda at foo.cc:9:30),
std::__1::__wrap_iter<int *> >' is implicitly deleted because field
'm_predicate' has a deleted copy assignment operator
Predicate m_predicate;
^
foo.cc:9:30: note: lambda expression begins here
auto some = all | filtered([] (int i) { return i % 2; });
^
我知道将我的 lambda 存储在一个std::function
修复它的地方,但我想避免付出代价。使用自定义包装器std::is_sorted
并不能解决问题。这个问题似乎与其他问题有关(例如,boost transform iterator 和 c++11 lambda),但事实并非如此——至少它们的解决方法不适用于这里。
谢谢!