如何避免代码重复实现const和非const迭代器? [英] How to avoid code duplication implementing const and non-const iterators?

查看:156
本文介绍了如何避免代码重复实现const和非const迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个具有类似STL的接口的自定义容器。我必须提供一个常规的迭代器和一个const迭代器。大多数迭代器的两个版本的代码是相同的。如何避免这种重复?

I'm implementing a custom container with an STL-like interface. I have to provide a regular iterator and a const iterator. Most of the code for the two versions of the iterators is identical . How can I avoid this duplication?

例如,我的容器类是 Foo ,我正在实现 FooIterator FooConstIterator 。这两个迭代器必须提供类似 operator ++()的方法。

For example, my container class is Foo, and I'm implementating FooIterator and FooConstIterator. Both of the iterators have to provide methods like operator++() which are identical.

a href =http://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-funct>如何删除代码类似的const和非const成员函数之间的复制?,但是那个的答案是特定于const和非const方法,特别是访问器。

My question is similar to How do I remove code duplication between similar const and non-const member functions?, but the answer to that one is specific to const and non-const methods, especially accessors. I don't see how that might generalize to the iterator problem.

我应该有 FooIterator FooConstIterator 并使用额外的非const方法扩展它?

Should I have FooIterator derive from FooConstIterator and extend it with additional non-const methods? That either leads to virtual methods or method hiding, which seem inappropriate here.

也许 FooIterator 应该包含一个 FooConstIterator 。虽然这种方法确实减少了实现的重复,似乎重新引入了很多样板方法定义。

Perhaps FooIterator should contain a FooConstIterator. Although that approach does reduce implementation duplication, it seems to re-introduce a lot of boilerplate method definitions.

有一个聪明的模板技术从单个定义生成两个迭代器?

Is there clever template technique for generating the two iterators from a single definition? Or perhaps there's a way to--shudder--use the preprocessor to stamp out these nearly identical classes.

我尝试查看我的本地STL实现,看看如何使用预处理器它处理这个。有很多帮助类,我在设计时遇到麻烦,但它看起来像功能是简单的复制。

I've tried looking at my local STL implementation to see how it handle this. There are so many helper classes that I'm having trouble grokking the design, but it looks like the functionality is simply duplicated.

在以前的项目中,我的自定义容器在标准的STL容器之上,所以我没有提供我自己的迭代器。

In previous projects, my custom container was built on top of a standard STL container, so I didn't have to provide my own iterators. That's not an option in this case.

推荐答案

您可以使用CRTP和一个通用库来注入方法必须在当前C ++中复制转换器),或者只使用预处理器(不需要颤抖;轻松处理转换器):

You can use CRTP and a common base to "inject" methods (but you still have to duplicate ctors in current C++), or just use the preprocessor (no shuddering required; handles ctors easily):

struct Container {

#define G(This) \
This operator++(int) { This copy (*this); ++*this; return copy; }
// example of postfix++ delegating to ++prefix

  struct iterator : std::iterator<...> {
    iterator& operator++();
    G(iterator)
  };
  struct const_iterator : std::iterator<...> {
    const_iterator& operator++();
    G(const_iterator)
  };

#undef G
// G is "nicely" scoped and treated as an implementation detail
};

使用std :: iterator,它提供的typedef以及您可能提供的任何其他typedef宏观直截了当。

Use std::iterator, the typedefs it gives you, and any other typedefs you might provide to make the macro straight-forward.

这篇关于如何避免代码重复实现const和非const迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆