Deciding execution path based on template parameters?



 DEVELOP > c-Plus-Plus > Deciding execution path based on template parameters?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Sachin Garg"
Date: 03 Sep 2006 11:55:36 PM
Object: Deciding execution path based on template parameters?
I was trying something with templates in C++, can't figure out if it is
possible.
Can I check in a function what template parameter was passed, and then
decide what to do?
for example...
I have two simple types:
struct x {int a, b, c};
struct y {int x};
A template class is defined with
template<class T>
then in one of the member functions, I want to do
if(T==x) { do something with a, b and c}
if(T==y) { do something with x}
(These ifs need to be resolved at compile time, else the code wont
compile.)
Something similar can be done through template specialization, but I
was hoping if such if/else is possible.
I can do if(typeid(T ) == typeid(x))
but this is resolved at run time... I want something which gets
resolved at compile time. To avoid the performance hit.
I can think of a work around....
template<int t_id, class T>
and then in code...
if(t_id==x_id) { do stuff for x}
if(t_id==y_id) { do stuff for y}
(here x_id and y_id are constants)
Only problem is that now t_id and T essentially mean the same thing, so
its kinda redundant to specify both. But this should work
Templates can have if else on value parametres which get solved at
compile time, I was just hoping if it can do that with type parameters
too.
Technically this seems to be possible. What will be the best option in
this case, any alternates you can suggest.
Sachin Garg [India]
www.sachingarg.com | www.c10n.info
.

User: "Jerry Coffin"

Title: Re: Deciding execution path based on template parameters? 04 Sep 2006 01:35:57 AM
In article <1157345736.877642.6300@m79g2000cwm.googlegroups.com>,
schngrg@gmail.com says...


I was trying something with templates in C++, can't figure out if it is
possible.

Can I check in a function what template parameter was passed, and then
decide what to do?

for example...

I have two simple types:

struct x {int a, b, c};
struct y {int x};

A template class is defined with
template<class T>

then in one of the member functions, I want to do

if(T==x) { do something with a, b and c}
if(T==y) { do something with x}

This is done somewhat differently. You start by writing a template that
works for types in general (though in some cases, the general version is
never intended to be used, and may intentionally cause an error so it
_can't_ be used).
Then you write a specialization for the types that receive special
treatement:
template <class T>
class XYZ {
// whatever
};
template <>
class XYZ<x> {
// do something with a, b, c
};
template <>
class XYZ<y> {
// do something with x
};
This is handled by compile time -- but it does NOT (directly) support
writing only _part_ of the template in a specialized fashion -- you have
to reproduce the specialized template in general. If you want to
specialize only part, you need to split that part out into a separate
template of its own.
--
Later,
Jerry.
The universe is a figment of its own imagination.
.
User: "Sachin Garg"

Title: Re: Deciding execution path based on template parameters? 04 Sep 2006 05:33:01 AM
Jerry Coffin wrote:

In article <1157345736.877642.6300@m79g2000cwm.googlegroups.com>,
schngrg@gmail.com says...


I was trying something with templates in C++, can't figure out if it is
possible.

Can I check in a function what template parameter was passed, and then
decide what to do?

for example...

I have two simple types:

struct x {int a, b, c};
struct y {int x};

A template class is defined with
template<class T>

then in one of the member functions, I want to do

if(T==x) { do something with a, b and c}
if(T==y) { do something with x}


This is done somewhat differently. You start by writing a template that
works for types in general (though in some cases, the general version is
never intended to be used, and may intentionally cause an error so it
_can't_ be used).

Then you write a specialization for the types that receive special
treatement:

template <class T>
class XYZ {
// whatever
};

template <>
class XYZ<x> {
// do something with a, b, c
};

template <>
class XYZ<y> {
// do something with x
};

This is handled by compile time -- but it does NOT (directly) support
writing only _part_ of the template in a specialized fashion -- you have
to reproduce the specialized template in general. If you want to
specialize only part, you need to split that part out into a separate
template of its own.

My goal behind using templates here is to avoid duplicated code :-)
I guess I will try using the t_id approach I mentioned and see how it
goes.
Thanks,
Sachin Garg [India]
www.sachingarg.com | www.c10n.info
.
User: "Jerry Coffin"

Title: Re: Deciding execution path based on template parameters? 04 Sep 2006 11:07:26 AM
In article <1157365978.268570.32640@i3g2000cwc.googlegroups.com>,
schngrg@gmail.com says...
[ ... ]

My goal behind using templates here is to avoid duplicated code :-)

Right -- if you only have a few things in a few places that depend on
the type, you can still use templates, but factor those few bits into
their own functions (or functors) and supply that as a template
parameter:
struct x { int a, b, c; };
strut y { int x; };
struct x_spec {
void operator()(x const &xx) {
// do something with x
}
};
struct y_spec {
void operator()(y const &yy) {
// do something with y
}
};

template <class T, class special>
class whatever {
T t;
public:
void member1() {
special()(t); // code specialized for one type
}
void member2() {
// code for either type
}
};
This depends, however, on being able to factor the specialized behavior
into a relatively small number of specialized functions. If you have
lots of different type-dependencies spread throughout the code, it's
probably not going to work very well -- but then again, that starts to
sound like what you have may not have enough commonality to really gain
a lot from code sharing in any case. In a case like that, instead of a
class template, you might be better off with a few function templates to
provide a common code base for the things that really are the same
across the types.
--
Later,
Jerry.
The universe is a figment of its own imagination.
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER