Help! Passing Templates functions to template functions

Discussion in 'C++' started by ILLOGIC, Jun 1, 2004.

  1. ILLOGIC

    ILLOGIC Guest

    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
     
    ILLOGIC, Jun 1, 2004
    #1
    1. Advertising

  2. ILLOGIC wrote in news: 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/
     
    Rob Williscroft, Jun 1, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    481
  2. recover
    Replies:
    2
    Views:
    817
    recover
    Jul 25, 2006
  3. Replies:
    1
    Views:
    332
    John Harrison
    Feb 21, 2007
  4. Hicham Mouline
    Replies:
    0
    Views:
    436
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page