Sign in to follow this  
dmatter

iterator_category template specialisation *solved*

Recommended Posts

NOTE: I solved this myself simply using template partial specialisation on the iterator category. I simply thought there was a better way but I realised there isn't a much cleaner alternative. Actually the code below doesn't work anyway (because of obvious reasons with the template parameters). What is the best method to specialise a class based on iterator category when the container type is passed as a template argument? The reason being to exlude functions from compilation. Queue simplified example:
template <class it, std::input_iterator_tag>
struct impl_iterator_wrapper
{
    it impl_iter;

//Implements:
    //operator ++
};

template <class it, std::random_access_iterator_tag>
struct impl_iterator_wrapper
{
    it impl_iter;

//Implements:
   //operator ++
   //operator --
   //operator +=
   //operator -=
};

template <class Tp>
struct iterator_wrapper :
public imp_iterator_wrapper<typename Tp::iterator,
                            std::iterator_traits<typename Tp::iterator>::iterator_category>
{
    iterator_wrapper (typename Tp::iterator it) : impl_iter(it) { }
};


template <class Tp>
class vector_wrapper
{
private:
    std::vector<Tp> impl;

public:
    typedef iterator_wrapper<std::vector<Tp> > iterator;

    iterator begin() const { return iterator(impl.begin()); }
};
Now don't laugh at the utter pointlessness of this code, its just demonstrates what I mean. Assuming I've done it right this example shows how I could make a wrapper for an iterator which has the correct interface for the capabilities of the internal iterator. So a random access iterator has the abilities to jump arbitrarily around, therefore the wrapper should too. I'm not convinced however if this is the best way to achieve this. Any suggestions or pointers are welcome. Thanx. [Edited by - dmatter on June 3, 2005 4:11:52 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this