473,387 Members | 1,798 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,387 software developers and data experts.

a little template help

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
4 1238

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
5
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.