In article <du**********@gander.coarse.univie.ac.at>,
pi****@unet.univie.ac.at (Piotr Sawuk) wrote:
What is the proper c++-way of saying:
template <class Container_>
struct forwards_iterator_toggle
{
typedef Container_ Container;
typedef typename Container::iterator iterator;
typedef typename Container::reverse_iterator reverse_iterator;
typedef backwards_iterator_toggle<Container_> Other;
template<class rIter_>
static typename Other::rIter_ toggle(const rIter_ i)
{return --(i.base());}
};
template <class Container_>
struct backwards_iterator_toggle
{
typedef Container_ Container;
typedef typename Container::reverse_iterator iterator;
typedef typename Container::iterator reverse_iterator;
typedef forwards_iterator_toggle<Container_> Other;
template <typename rIter_>
static typename Other::rIter_ toggle(const rIter_ i)
{return --(typename Other::rIter_)(i);}
};
#include<list>
int main(...)
{
typedef std::list<int> myList;
typedef backwards_iterator_toggle<myList> myToggle;
myList list;
typename myToggle::iterator i=
myToggle::toggle(list.begin());
}
Are you still trying to make an iterator that can change direction at
will? Here you go (it even uses the State pattern rather than
conditionals):
template < typename BiIt >
class reversable_iterator
{
struct direction {
virtual void increment( BiIt& it ) = 0;
virtual void decrement( BiIt& it ) = 0;
};
struct foward : direction {
virtual void increment( BiIt& it ) { ++it; }
virtual void decrement( BiIt& it ) { --it; }
} fwrd;
struct backward : direction {
virtual void increment( BiIt& it ) { --it; }
virtual void decrement( BiIt& it ) { ++it; }
}bckwrd;
direction* which_dir;
BiIt rep;
public:
reversable_iterator( BiIt it ): which_dir( &fwrd ), rep( it ) {
}
typename BiIt::value_type& operator*() { return *rep; }
reversable_iterator& operator++() {
which_dir->increment( rep );
return *this;
}
reversable_iterator& operator--() {
which_dir->decrement( rep );
return *this;
}
void reverse() {
which_dir = ( which_dir == &fwrd ) ?
static_cast<direction*>( &bckwrd ) :
static_cast<direction*>( &fwrd );
}
};
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.