Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help! Passing Templates functions to template functions

Reply
Thread Tools

Help! Passing Templates functions to template functions

 
 
ILLOGIC
Guest
Posts: n/a
 
      06-01-2004
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
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      06-01-2004
ILLOGIC wrote in news: 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Copying a template class into another template class object with different no of templates erictham115@gmail.com C++ 1 02-21-2007 05:58 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57