I’m writing a random access container in C++. In my code I use this (well, in my real code I use all kinds of Allocator typedefs, this is just easier to understand):
template<typename T, typename Allocator = std::allocator<T> >
class Carray {
public:
// ...
typedef T* iterator;
typedef const T* const_iterator;
// ...
};
But I can also create a different iterator class derived from std::iterator. This would add support for typedefs (it::iterator_category, it::difference_type, etc).
Now my question, is there an overhead by using a iterator class instead of a raw pointer? If yes, how substantial is this overhead, and is it severe enough to not use a iterator class?
You have iterator category, difference type etc avalaibale for you even if you have a raw pointer. You see, there is this
iterator_traits<>template which you should use. It is already specialized for pointers.