C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can templates be used to call multiple template function wit

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jordan Samuels
Guest





PostPosted: Thu Jan 22, 2004 9:15 am    Post subject: Can templates be used to call multiple template function wit Reply with quote



I am wondering if there is a way to use templates in a clever way to
provide an elegant to call multiple templatized functions with
multiple parameter types.

Imagine I have two arithmetic functions which can work on a variety of
primitive types, eg.

template<class T> T fun1(T x) { return x; }
template<> double fun1<double>(double x) { return x * 2.0; }

template<class T> T fun2(T x) { return 4 * x; }
template<> double fun2<double>(double x) { return x * 8.0; }

Assume I want to call these functions on ints, floats, and doubles (in
real life they would of course be more interesting; maybe I want to
test chip precision for these built-in types).

Now it's easy to call both for a fixed type, or one for a variety of
types:

void do_both_with_double(double x) {
cout << "fun1( (double) " << x << ") = " << fun1(x) << endl;
cout << "fun2( (double) " << x << ") = " << fun2(x) << endl;

}

void do_fun1_with_all(double x) {
cout << "fun1( (double) " << x << ") = " << fun1( x) << endl;
cout << "fun1( (float) " << x << ") = " << fun1((float)x) << endl;
cout << "fun1( (int) " << x << ") = " << fun1((int) x) << endl;
}

Then if I wanted to get ugly I could just create a do_fun2_with_all
and cut/paste the code, replacing "fun1" with "fun2". Or if I was
feeling very perverse I could use a macro.

Is there a clever way to do this with templates? Could
do_fun1_with_all become a templatized function that accepted the
template family fun1<> or fun2<> as a parameter. Something like:

template< template< class > Function >
void do_fun1_with_all(double x) {
cout << Function cout << Function // ..
}

I realize that this last snippet of code doesn't really make sense --
is there a way for it to or is this just not a possibility within C++?
I am admittedly naive about the full power of templates but from my
limited reading I see templates providing so much genericity it seems
"morally believable" that they'd be able to help here.

Thanks very much.

Jordan Samuels

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Thu Jan 22, 2004 2:59 pm    Post subject: Re: Can templates be used to call multiple template function Reply with quote



In message <8e4648be.0401212302.4bba5a10 (AT) posting (DOT) google.com>, Jordan
Samuels <jordan (AT) madsam (DOT) com> writes
Quote:
I realize that this last snippet of code doesn't really make sense --
is there a way for it to or is this just not a possibility within C++?
I am admittedly naive about the full power of templates but from my
limited reading I see templates providing so much genericity it seems
"morally believable" that they'd be able to help here.

I think you are fishing around for template template parameters, but you
may also get some mileage from function reference parameters because a
fully specialised function template is a function.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Christian Jaeger
Guest





PostPosted: Fri Jan 23, 2004 9:50 am    Post subject: Re: Can templates be used to call multiple template function Reply with quote



If your compiler supports partial specialisations, you can do many
things, (see example below). - Whether thtat's clever or not - I leave
to you Smile.

#include <iostream>

template<typename T,unsigned n> T fun(T const & x) {
std::cout << "fun with " << typeid(T).name() << 't';
return n*x;
}

template
template<typename T,unsigned k> struct DoIt:public DoIt<T,k-1>{
DoIt(T const & x):DoIt<T,k-1>(x){
std::cout << fun }
};

template
template<typename l, typename r, unsigned k> struct
DoIt<L:public DoIt<l,k>, public DoIt<r,k>{
DoIt(l const & x): DoIt<l, k >(x), DoIt<r,k >(x){}
};

int main(){

typedef L<int, L > types;
const unsigned k = 5;

DoIt<types,k> foo(10);
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Attila Feher
Guest





PostPosted: Fri Jan 23, 2004 8:55 pm    Post subject: Re: Can templates be used to call multiple template function Reply with quote

Jordan Samuels wrote:
Quote:
I am wondering if there is a way to use templates in a clever way to
provide an elegant to call multiple templatized functions with
multiple parameter types.
[SNIP]


As you have seen in Christians post, you can use typelists. He did not name
them as such, but they are. You can google for typelist or go to
http://www.moderncppdesign.com and get Andrei's excellent book by one click
from there. Thanks god the typelists are at the beginning of the book,
before the point where you feel that your whole world is changing. I mean
you will already feel it that point, but it is not the real one yet. Smile
The book is very good, it provides a new view into programming in C++ - what
I meant with my comments is that typelist is in the less complex, sort of
introductory, part of the book. And it is very simple to understand.

Once you have typelists, you can do many-many nice things with it.

--
Attila aka WW



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.