mercredi 26 août 2020

Template Class Inside Template Class - Different Parameter

Is it possible to create a template class inside a template class like the following

// Container.h
template <typename T>
class Container
{
private:
  using iterator = Iterator<Node<T>>;
  using const_iterator = Iterator<const Node<T>>;

  // Node struct to hold data.
  template <typename T>
  struct Node
  {
    T data_;
  };

public:
  // Templated iterator for const or non-const.
  template <typename NodeType>
  class Iterator
  {
  private:
    NodeType* node_;
  
  public:
    Iterator();
  };
};

#include "Container.tpp"

So here I declare a template for an iterator that takes in a NodeType which is different from the T that the container class template takes.

If this is possible to do, how do I implemenet the Iterator() inside a different file? Something like

// Container.tpp
template <typename T>
LinkedList<T>::LinkedListIterator<NodeType>::Iterator()
{
  // Implementation ...
}

This does not seem right since I do not have access to the NodeType. Any advice would be appreciated.

Aucun commentaire:

Enregistrer un commentaire