Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can templates be used to call multiple template function with multiple argument types?

3 views
Skip to first unread message

Jordan Samuels

unread,
Jan 22, 2004, 4:15:01 AM1/22/04
to
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<double> (x) << endl;
cout << Function<float> (x) << endl;
// ..
}

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! ]

Francis Glassborow

unread,
Jan 22, 2004, 9:59:14 AM1/22/04
to
In message <8e4648be.04012...@posting.google.com>, Jordan
Samuels <jor...@madsam.com> writes

>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

Christian Jaeger

unread,
Jan 23, 2004, 4:50:54 AM1/23/04
to
If your compiler supports partial specialisations, you can do many
things, (see example below). - Whether thtat's clever or not - I leave
to you :).

#include <iostream>

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

template<typename l, typename r> struct L{};

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

template<typename T> struct DoIt<T,0>{DoIt(T const & x) {} };

template<typename l, typename r, unsigned k> struct
DoIt<L<l,r>,k>: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<unsigned, L<float, double> > > types;
const unsigned k = 5;

DoIt<types,k> foo(10);

Attila Feher

unread,
Jan 23, 2004, 3:55:59 PM1/23/04
to
Jordan Samuels wrote:
> 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. :-)
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

0 new messages