pointer to function template parameter

Discussion in 'C++' started by Amadeus W. M., Sep 3, 2005.

  1. Don't laugh/get pissed off if I'm way off here, but is it possible to have
    a template parameter a pointer to a function? If it's possible, an example
    would be good.

    I'm getting tired of implementing functors as wrappers for what should be
    simple C functions.
     
    Amadeus W. M., Sep 3, 2005
    #1
    1. Advertising

  2. Hello Amadeus W. M.,

    > Don't laugh/get pissed off if I'm way off here, but is it possible to
    > have a template parameter a pointer to a function? If it's possible,
    > an example would be good.
    >
    > I'm getting tired of implementing functors as wrappers for what should
    > be simple C functions.
    >


    You should be able to pass a pointer to a function where you pass a functor.

    example:

    template< typename T > void foo( T fun )
    {
    fun();
    }

    void bar()
    {
    }

    struct functor
    {
    void operator()();
    };

    main()
    {
    foo(bar); // pass pointer to function
    foo(functor()); // pass functor instance
     
    Thierry Miceli, Sep 3, 2005
    #2
    1. Advertising

  3. Amadeus W. M.

    benben Guest

    "Amadeus W. M." <> wrote in message
    news:p...
    > Don't laugh/get pissed off if I'm way off here, but is it possible to have
    > a template parameter a pointer to a function? If it's possible, an example
    > would be good.
    >
    > I'm getting tired of implementing functors as wrappers for what should be
    > simple C functions.



    AFAIK a simple C function is a valid functor.

    Ben
     
    benben, Sep 3, 2005
    #3
  4. Amadeus W. M. wrote:
    > Don't laugh/get pissed off if I'm way off here, but is it possible to have
    > a template parameter a pointer to a function? If it's possible, an example
    > would be good.
    >
    > I'm getting tired of implementing functors as wrappers for what should be
    > simple C functions.
    >


    Is this what you mean?

    #include <amth.h>

    template <double (*func)(double)>
    double wierd_trig(double x)
    {
    return func(x);
    }

    int main()
    {
    wierd_trig<sin>(1.0);
    wierd_trig<cos>(1.0);
    }

    Template terminology is confusing, but I believe that is what you asked.

    john
     
    John Harrison, Sep 3, 2005
    #4
  5. On Sat, 03 Sep 2005 05:48:08 +0000, John Harrison wrote:

    > Amadeus W. M. wrote:
    >> Don't laugh/get pissed off if I'm way off here, but is it possible to have
    >> a template parameter a pointer to a function? If it's possible, an example
    >> would be good.
    >>
    >> I'm getting tired of implementing functors as wrappers for what should be
    >> simple C functions.
    >>

    >
    > Is this what you mean?
    >
    > #include <amth.h>
    >
    > template <double (*func)(double)>
    > double wierd_trig(double x)
    > {
    > return func(x);
    > }
    >
    > int main()
    > {
    > wierd_trig<sin>(1.0);
    > wierd_trig<cos>(1.0);
    > }
    >
    > Template terminology is confusing, but I believe that is what you asked.
    >
    > john


    That's precisely what I mean, thanks a lot!
     
    Amadeus W. M., Sep 7, 2005
    #5
    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. Vijai Kalyan
    Replies:
    4
    Views:
    724
    Vijai Kalyan
    Nov 8, 2005
  2. MaxMax
    Replies:
    3
    Views:
    1,668
    MaxMax
    May 28, 2006
  3. ank
    Replies:
    4
    Views:
    1,095
    James Kanze
    Nov 1, 2006
  4. Stuart Redmann
    Replies:
    5
    Views:
    494
    Stuart Redmann
    Dec 14, 2007
  5. avasilev
    Replies:
    4
    Views:
    585
    avasilev
    Dec 22, 2011
Loading...

Share This Page