473,412 Members | 3,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

templates instead of macros

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());
}
--
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...

P
Mar 11 '06 #1
2 1573
Piotr Sawuk wrote:
What is the proper c++-way of saying:


I don't really know what it is you are trying to say. I may have got
lost in the excess of typedefs that obviously won't work.

Perhaps a short explanation of what you are trying to achieve would be
useful.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 11 '06 #2
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.
Mar 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andreas Micheler | last post by:
Hi, I have several long complex C macros in the math module of aUCBLogo like #define _XFUNC_primitive(NAME)\ NodeP _##NAME(PairPP arg)\ { ((ObjectP)(arg))->var()->NAME( arg );\ return...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.