Simon
Simon

Reputation: 1684

Store list iterator as member

I have a legacy library I'm currently modernizing.

This library has a custom made list class, which has several issues in safety and performance.

Therefor I want to use std::list under the hood of the list class.

Unfortunately the API of the list class relies heavily on the current position in the list. I don't want to change that API, because this would require a lot of rework and test in the application using the library.

I tried to store the list and the iterator with the current position as member variables of the templated class, but apparently it's not possible to have a templated iterator as member variable.

Any suggestions how this could be solved?

This is my class

template <class T> 
class MyList
{
  public:
    explicit MyList(const T &t)
    {
        list.push_back(t);
        it = list.begin();
    }

    int size() const { return list.size(); }

    void copy(const MyList<T> &orig)
    {
        list = orig;
    }

    int insert(MyList<T> *t, MyListBASE::Location where = MyListBASE::AFTER)
    {
        list.insert(it, t);
        return 0;
    }

    int insert(const T &t, MyListBASE::Location where = MyListBASE::AFTER)
    {
        list.insert(it, t);
        return 0;
    }

    T *head() { return list.front(); }
    T *tail() { return list.back(); }
    T *current() { return it; }
    T* next() { return std::next(it); }
    T* prev() { return std::prev(it); }

  protected:
    std::list<T>::iterator it; // seems not to be possible
    std::list<T> list;
};

Upvotes: 0

Views: 85

Answers (1)

Richard Critten
Richard Critten

Reputation: 2145

std::list<T>::iterator is a dependent name (of the template parameter). So the compiler needs to know that iterator is a type and not a variable:

typename std::list<T>::iterator it;

Will give the compiler enough information to fix the problem.

Upvotes: 3

Related Questions