a little template help

T

toton

I have a code like this
typedef unsigned int uint;
class PtuInfo{
bool increasing_;
uint ptIndex_;
uint begin_;
uint end_;
public:
PtuInfo(uint begin,uint end,bool back):
increasing_(!back),
begin_(begin),end_(end){
if(increasing_)
ptIndex_ = begin_;
else
ptIndex_ = end_;
}
void next(){
if(increasing_)
ptIndex_++;
else
ptIndex_--;
}
bool isEnd(){
if(increasing_){
return ptIndex_ == end_;
}
else{
return ptIndex_ == begin_;
}
}
};

Now, as it can be seen, the operations next() & isEnd() can be
statically evaluated, as the increasing_ field is provided during
construction of the object.
Thus it can be split into two class, which dont need to check
increasing_ parameter on each call.
I feel a better way to do it using a template param of bool type, to
avoid code duplication (the class do some other things also, but all
operations are based on the field increasing_ which is passed during
construction only).
Can anyone show a static template equivalent of the above code ?

Thanks in advance
abir
 
K

kwikius

toton said:
I have a code like this
typedef unsigned int uint;
class PtuInfo{
bool increasing_;
uint ptIndex_;
uint begin_;
uint end_;
public:
PtuInfo(uint begin,uint end,bool back):
increasing_(!back),
begin_(begin),end_(end){
if(increasing_)
ptIndex_ = begin_;
else
ptIndex_ = end_;
}
void next(){
if(increasing_)
ptIndex_++;
else
ptIndex_--;
}
bool isEnd(){
if(increasing_){
return ptIndex_ == end_;
}
else{
return ptIndex_ == begin_;
}
}
};
Can anyone show a static template equivalent of the above code ?

Just make 'Increasing' a template parameter:
// forward declaration of the class with no definition
template <bool Increasing> class PtuInfo;

// create a specialisation for each case of Increasing:

template<>
class PtuInfo<true>{
/* add what applies here */
};

template<>
class PtuInfo<false>{
/* add what applies here */
};

regards
Andy Little
 
T

toton

kwikius said:
Just make 'Increasing' a template parameter:
// forward declaration of the class with no definition
template <bool Increasing> class PtuInfo;

// create a specialisation for each case of Increasing:

template<>
class PtuInfo<true>{
/* add what applies here */
};

template<>
class PtuInfo<false>{
/* add what applies here */
};

regards
But a class template specialization still needs to have 2 class
implementation fully.
Instead, what I was looking for specializing the functions only which
uses "increasing". not the other functions, like uint PtuInfo::begin()
which just returns begin().

Thus looking for how to specialize a member function of the template
class for the parameter.
instead of the whole class.
Thanks.
 
K

kwikius

toton said:
Thus looking for how to specialize a member function of the template
class for the parameter.
instead of the whole class.


typedef unsigned int uint;
class PtuInfo{
/*...*/
uint ptIndex_;
public:
template <bool I>
void next();
};

template <> void PtuInfo::next<true>()
{
ptIndex_++;
}

template <> void PtuInfo::next<false>()
{
ptIndex_--;
}

regards
Andy Little
 
T

toton

kwikius said:
typedef unsigned int uint;
class PtuInfo{
/*...*/
uint ptIndex_;
public:
template <bool I>
void next();
};

template <> void PtuInfo::next<true>()
{
ptIndex_++;
}

template <> void PtuInfo::next<false>()
{
ptIndex_--;
}
Thanks a lot. Exactly this is what I needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
BettinaPol

Latest Threads

Top