Jump to content
  • Advertisement
Sign in to follow this  

iterator_category template specialisation *solved*

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!