inheriting template function.

Sorry if my English or my coding conventions are bad.
std::list is the STL list.
I am trying to make a library that will use std::list<>::iterator and std::list<>::reverse_iterator seamlessly to the user according to his choice.
First, I wouldn't like to implement all std::list<>::iterator methods. So I'll inherit them to my class:
 
template <class realIter> class directedListTypeIter : public realIter

The user will instantiate the directedListTypeIter with 2 possible templates:
1
2
directedListTypeIter<std::list<value>::iterator>
directedListTypeIter<std::list<value>::reverse_iterator>

It means that I should create the following assignment operators (or copy constructor):
1
2
3
4
5
6
7
8
directedListTypeIter& operator=(std::list<value>::reverse_iterator &constructorIter) {
  ((std::list<value>::reverse_iterator*)this)->operator=(constructorIter);
  return *this;
}
directedListTypeIter& operator=(std::list<value>::iterator &constructorIter){
  ((std::list<value>::reverse_iterator*)this)->operator=(constructorIter);
  return *this;
}

I need all this assignment operators for the following function implementation, and other similar implementations.
1
2
3
4
5
6
7
8
9
directedListTypeIter begin(std::list<value>& userList){ 
  directedListTypeIter<realIter> retVal;
  if (someCheck()){
    retVal = userList.begin();
  } else {
    retVal = userList.rbegin();
  }
  return retVal;
}

In different words- In order to implement the begin() function, and other similar functions I need, I should implement the assignment operator (or copy constructor).

Here is my questions:
1. All this depends on the existence and usage of the "value" struct. How can I remove this dependency? I mean- remove "value" from that library to make it more generic.

2. If I had more possible inherited types, should I have been creating new assignment operator (or copy constructor) for each type? isn't the template programming meant not to copy that code? How can I make the same without copying the code to each operator?

3. What should I add so the following:
1
2
3
4
directedListTypeIter& operator=(realIter &constructorIter){
  ((realIter*)this)->operator=(constructorIter);
  return *this;
}

would compile- without adding something that lists all possible inherited values?

thanku.

p.s. This topic is about how to template the inheritance, and not about achieving the use of both iterator types in one interface. I mean- the "C++ Standard Library list" use case is just an example to better explain my issue.

Last edited on
Topic archived. No new replies allowed.