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

Help! Passing Templates functions to template functions

P: n/a
Hello,
i am beginner in c++. I hope tobe sufficiently clear and that someone
could help me on this topic. For example i have template function

<typename T> T sin_func(T & x){return sin(x);}

could it be possible that the paramenter of the function is another
template function?
An example would be very helpful

many thanks

max
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ILLOGIC wrote in news:cb**************************@posting.google.c om in
comp.lang.c++:
Hello,
i am beginner in c++. I hope tobe sufficiently clear and that someone
could help me on this topic. For example i have template function

<typename T> T sin_func(T & x){return sin(x);}

could it be possible that the paramenter of the function is another
template function?
An example would be very helpful


#include <iostream>
#include <ostream>
#include <cmath>
template < typename T >
T sin_func( T const &x )
{
return std::sin( x );
}
template < typename T >
T sin_func_func( T (*sin_func)( T const & ), T const &x )
{
return sin_func( x );
}
int main()
{
std::cout
<< sin_func_func( sin_func< double >, 3.142 / 4 )
<< std::endl
;

/* Alternativly:

sin_func_func< double >( sin_func, 3.124 / 4 );

*/

/* If you have g++ this also works:

sin_func_func( sin_func, 3.142 / 4 );

- i.e. gcc/g++ can deduce T from the second paramiter.
*/
}

I'm 95% sure that the third (g++) commented out invocation in
main() should work (i.e. its standard) but use either of the
other versions for maximum portability (and also when you
haven't got a 'T' paramiter).

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.