“通用” c++ 中的迭代器
我有:
void add_all_msgs(std::deque<Message>::iterator &iter);
如何使该函数“通用”,以便它可以采用任何类型的输入迭代器? 我并不关心它是在迭代双端队列、向量还是其他东西,只要迭代器在迭代消息即可。 - 这在 C++ 中完全可能吗?
I have:
void add_all_msgs(std::deque<Message>::iterator &iter);
How can I make that function "generic", so it can take any kind of inputiterators ? I don't really care if it's iterating a deque,a vector or something else, as long as the iterator is iterating Message's. - is this at all straight forward possible in c++ ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
用法 :
需要指定结束,否则不知道什么时候停止! 它还使您可以灵活地仅添加容器的子范围。
usage :
You need to specify the end, otherwise you won't know when to stop! It also gives you the flexibility of adding only a subrange of a container.
用法:
Usage:
如果您希望编译器检查迭代器是否确实引用
Message
对象,您可以使用如下技术。If you want the compiler to check whether the iterator actually refers to
Message
objects, you can use a technique like the following.如果您不想模板化 add_all_msgs 函数,可以使用 adobe::any_iterator:
If you don't want to templatize your add_all_msgs function, you can use adobe::any_iterator:
使用 C++ 风格的迭代器很难实现动态多态性。
operator++(int)
按值返回,据我所知这是很棘手的:你不能有一个虚拟成员函数在不被切片的情况下按值返回*this
。如果可能的话,我建议像其他人所说的那样使用模板。
但是,如果您确实需要动态多态性,例如因为您无法像模板那样公开 add_all_msgs 的实现,那么我认为您可以假装是 Java,如下所示:
我已经检查过它是否可以编译,但我还没有没有测试过,而且我以前从未使用过这种设计。 我也没有担心 const 性 - 实际上你可能需要一个 const Message &get() const 。 目前适配器无法知道何时停止,但是您开始使用的代码也不知道,所以我也忽略了这一点。 基本上,您需要一个
hasNext
函数,它将wrapped
与提供给构造函数的结束迭代器进行比较。您也许可以使用模板函数和常量引用做一些事情,这样客户端就不必了解或声明那个令人讨厌的适配器类型。
[编辑:想一想,最好有一个存根
add_all_msgs
函数模板,将其参数包装在适配器中,然后调用real_add_all_msgs
。 这对客户端完全隐藏了适配器。]It's difficult to have dynamic polymorphism with C++-style iterators.
operator++(int)
returns by value, which afaik is intractable: you can't have a virtual member function which returns*this
by value without it being sliced.If possible, I recommend using templates as everyone else says.
However if you do need dynamic polymorphism, for example because you can't expose the implementation of add_all_msgs as a template would do, then I think you could pretend to be Java, like this:
I've checked that this compiles, but I haven't tested it and I've never used this design before. I also haven't bothered with const-ness - in practice you probably want a
const Message &get() const
. And at the moment the adaptor has no way of knowing when to stop, but then neither does the code you started with, so I've ignored that too. Basically you'd need ahasNext
function which compareswrapped
against an end iterator supplied to the constructor.You might be able to do something with a template function and const references, so that the client doesn't have to know about or declare that nasty Adaptor type.
[Edit: come to think of it, it's probably better to have a stub
add_all_msgs
function template, that wraps its parameter in an Adaptor and then callsreal_add_all_msgs
. This completely hides the adaptor from the client.]比上面的稍微简单一些(因为它利用了现有的库):
Slightly simpler that the above (in that it leverages existing libraries):