Help! Passing Templates functions to template functions

I

ILLOGIC

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
 
R

Rob Williscroft

ILLOGIC wrote in 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top