AnyIterator 和 boost 迭代器外观
是否可以使用 boost 迭代器外观实现任何迭代器? 我不想在我的基类中定义实现细节
class Base
{
public:
typedef std::vector<int>::iterator iterator;//implementation detail
...
virtual iterator begin()=0;
virtual iterator end()=0;
};
,或者我是否必须完全从头开始编写一个;
Is it possible to implement an any iterator with boost iterator facade?
I don't want to define implementation details in my baseclass
class Base
{
public:
typedef std::vector<int>::iterator iterator;//implementation detail
...
virtual iterator begin()=0;
virtual iterator end()=0;
};
or do i have to write one completely from scratch;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的代码已修复从
Base
返回的迭代器类型及其对std::vector::iterator
的所有实现,这可能不是您想要的想。 Jeremiah 的建议是一种方法,但有一个缺点:你失去了与 STL 的兼容性...我知道多态迭代器包装器的三种实现:any_iterator
(它实现boost::iterator_facade< /code>)
any_iterator
的层次结构。这个问题比看起来更难......我自己尝试了一下,主要是因为我需要
any_iterators
类型参数中的协变(any_iterator
应该自动转换为 < code>any_iteratorEnumerator
这样的 AC# 更容易实现(*)(恕我直言,通常是比类似 STL 的迭代器对更清晰的概念),但同样,您“失去”了 STL。(*) = 当然没有“产量”:-)
The code you've posted has fixed the type of iterators returned from
Base
and all it's implementantions tostd::vector<int>::iterator
which is probably not what you want. Jeremiah's suggestion is one way to go with one drawback: you loose compatibility with STL... I know of three implementations of a polymorphic iterator wrapper:any_iterator
(which implementsboost::iterator_facade
)opaque_iterator
library (google for it), orany_iterator
s.The problem is harder than it might seem... I made an attempt myself mainly because I needed covariance in
any_iterators
type argument (any_iterator<Derived>
should be automatically convertible toany_iterator<Base>
) which is difficult to implement cleanly with STL like iterators. A C# likeEnumerator<T>
is easier to implement(*) (and imho generally a cleaner concept than STL-like pairs of iterators) but again, you "loose" the STL.(*) = without 'yield' of course :-)
我想这可能就是您正在寻找的:
以下是该页面的片段:
I think this may be what you're looking for:
Here's a snippet from that page::