By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

a little template help

P: n/a
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

Dec 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

toton wrote:
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 Increasingclass 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

Dec 21 '06 #2

P: n/a

kwikius wrote:
toton wrote:
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 Increasingclass 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.
Andy Little
Dec 21 '06 #3

P: n/a

toton wrote:
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

Dec 21 '06 #4

P: n/a

kwikius wrote:
toton wrote:
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_--;
}
Thanks a lot. Exactly this is what I needed.
regards
Andy Little
Dec 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.