我有一个类,将一些Boost转换适配器应用于范围(出于示例的原因,实际上,它比这复杂得多):
struct Foo {
auto range() const {
return boost::irange(0, 10)
| boost::adaptors::transformed([] (auto x) { return x * 2; });
}
auto begin() const { return range().begin(); }
auto end() const { return range().end(); }
};
仅此一项,我们就可以使用以下范围来迭代Foo
:
for (auto x : Foo()) {
std::cout << num << std::endl;
}
但是,这与其他升压适配器或范围操作(例如boost::join
)不能很好地结合在一起:
auto bad = boost::join(boost::irange(0, 10), Foo());
auto also_bad = Foo() | boost::adaptors::transformed([] (auto x) { return x + 1; });
以上两种方法均会引起一些令人讨厌的模板错误。前者(bad
):
In file included from test.cpp:4:
/usr/local/include/boost/range/join.hpp:30:70: error: no type named 'type' in
'boost::range_iterator<const Foo, void>'
BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange1>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/usr/local/include/boost/range/join.hpp:44:28: note: in instantiation of template class
'boost::range_detail::joined_type<const Foo, const boost::integer_range<int> >' requested
here
: public range_detail::joined_type<SinglePassRange1, SinglePassRange2>::type
^
test.cpp:34:16: note: in instantiation of template class 'boost::range::joined_range<const Foo,
const boost::integer_range<int> >' requested here
auto bad = boost::join(Foo(), range);
^
...
而后者(also_bad
):
In file included from test.cpp:1:
In file included from /usr/local/include/boost/range/any_range.hpp:17:
In file included from /usr/local/include/boost/range/detail/any_iterator.hpp:22:
In file included from /usr/local/include/boost/range/detail/any_iterator_wrapper.hpp:16:
In file included from /usr/local/include/boost/range/concepts.hpp:24:
/usr/local/include/boost/range/value_type.hpp:26:70: error: no type named 'type' in
'boost::range_iterator<Foo, void>'
struct range_value : iterator_value< typename range_iterator<T>::type >
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/usr/local/include/boost/range/adaptor/replaced.hpp:109:40: note: in instantiation of template
class 'boost::range_value<Foo>' requested here
BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f )
^
test.cpp:35:27: note: while substituting deduced template arguments into function template
'operator|' [with SinglePassRange = Foo]
auto also_bad = Foo() | boost::adaptors::transformed([] (auto x) { return x * 2; });
^
...
这两个错误似乎都在抱怨Foo
不是范围。我尝试为建议here添加operator OutContainer()
/ iterator
的{{1}}和typedef,但无济于事。我必须对const_iterator
做些什么才能使其在这些范围操作中正常发挥作用?
答案 0 :(得分:2)
在这种情况下,错误非常有用。您的类型必须建模SinglePassRange
,而不必。有一个page关于如何执行此操作,虽然您提供了begin()
和end()
,但没有为iterator
和const_iterator
提供类型别名。因此,您不会建模SinglePassRange
。
但是您的代码失败实际上是非常好的,因为它也是不好的。您有begin()
个呼叫range().begin()
和end()
个呼叫range().end()
。这些是不同范围中的迭代器。因此,无论如何,这都是未定义的行为-您无法满足SinglePassRange
的语义概念。
更简单的解决方案是直接使用Foo()
。这已经有效:
auto good = boost::join(boost::irange(0, 10), Foo().range());
auto also_good = Foo().range() | boost::adaptors::transformed([] (auto x) { return x + 1; });
这意味着您只需要编写一个函数:range()