Я наткнулся на следующий шаблонный класс для итерации по двоичному дереву, в котором есть шаблон внутреннего класса для итератора:
template <typename T> struct BinaryTree
{
Node<T>* root = nullptr;
explicit BinaryTree(Node<T>* const root)
: root{ root }, pre_order{ *this }
{
root->set_tree(this);
}
~BinaryTree() { if (root) delete root; }
template <typename U>
struct PreOrderIterator
{
Node<U>* current;
explicit PreOrderIterator(Node<U>* current)
: current(current)
{
}
bool operator!=(const PreOrderIterator<U>& other)
{
return current != other.current;
}
// no continuations in C++ (unlike C#)
PreOrderIterator<U>& operator++()
{
if (current->right)
{
current = current->right;
while (current->left)
current = current->left;
}
else
{
Node<T>* p = current->parent;
while (p && current == p->right)
{
current = p;
p = p->parent;
}
current = p;
}
return *this;
}
Node<U>& operator*() { return *current; }
};
typedef PreOrderIterator<T> iterator;
iterator end()
{
return iterator{ nullptr };
}
iterator begin()
{
Node<T>* n = root;
if (n)
while (n->left)
n = n->left;
return iterator{ n };
}
};
Но я не могу понять, почему было бы полезно создать дополнительный шаблон внутреннего класса для итератора: не будет ли имя типа итератора всегда T, что делает дополнительный шаблон избыточным?
Есть ли какое-то преимущество в этом подходе, которого я не получаю?