1

I'm writing a collection of containers and iterators for an environment where STL and Boost are off the table. I'm struggling somewhat with dealing with containers, such as a Vector, that can contain both value types and pointer types, when it comes to handling iterators and their return types - via a GetValue function, for example. It is further complicated by wanted to support const iterators. I have seen here about the definition of a series of traits: value_type, reference, pointer. My question is how are these used in the context of creating iterators? Essentially, given that I want

Object & Vector<Object>::Iterator::GetValue()
Object * Vector<Object*>::Iterator::GetValue() 
const Object & Vector<Object>::ConstIterator::GetValue()
const Object * Vector<Object*>::ConstIterator::GetValue()

How do value_type, reference, pointer factor into this?

Community
  • 1
  • 1
Steven
  • 619
  • 5
  • 24
  • Did you try looking at what `std::vector` does? Hint: `std::vector::pointer` is `int **`. – Potatoswatter Nov 29 '14 at 07:07
  • @Potatoswatter Yes, it is clear that std::vector::pointer is **. My question relates to how these are used in the context of creating the correct signature for iterator's return type. – Steven Nov 29 '14 at 07:48

1 Answers1

3

The member types of iterators aren't used for very much. Fortunately, you don't need to bother defining them. The std::iterator base class does it for you.

template< typename value_type >
struct my_vector_iterator
    : std::iterator< std::random_access_iterator_tag, value_type >
    …

template< typename value_type >
struct my_vector_const_iterator
    : std::iterator< std::random_access_iterator_tag, value_type const >
    …
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • As I mentioned, STL isn't available to me in these circumstances, but thank you for pointing out what's happening. In these cases is the 'value_type' just T or T*, if the container contains pointers to type t? – Steven Nov 29 '14 at 07:45
  • @Steven Yes, it's that simple. If the container contains pointers, its implementation will involve pointers to pointers but never (ever) the pointed-to type directly. – Potatoswatter Nov 29 '14 at 07:48
  • So for std::vector is the return type of the * operator on an iterator Object * & or Object * ? – Steven Nov 29 '14 at 07:55