Passing pointer to template function as argument to pointer to template function

Discussion in 'C++' started by Vijai Kalyan, Nov 8, 2005.

  1. Vijai Kalyan

    Vijai Kalyan Guest

    I was decomposing a task into different policies. Essentially, there is
    a general option obtained from a server and user options obtained from
    configuration variables. The two options are complementary to one
    another.

    So I proceeded to decompose the tasks that deal with user options into
    two functions. Each of the functions do something and towards the end
    they do supplementary tasks that depend on the server option.

    The whole things fits in a framework which notifies listeners before
    and after the main tasks and so the notification scheme for the
    listeners are separate policies as well.

    The whole thing is combinatorially complex and hence my decision to
    split things up using templates as policies. Here is where I run into
    my question: It is exemplified by the following simple code:

    #include <iostream>

    // main task function
    template<class FuncPtr>
    void Foo(FuncPtr ptr)
    {
    ptr();
    }

    // want to pass an instance of this to Foo
    template<class X>
    void Bar(X x)
    {
    x();
    }

    // want to pass pointer to this to Bar
    void G()
    {
    std::wcout << "From G!" << std::endl;
    }

    int main()
    {
    Foo(&Bar(&G)); // doesn't work, coz Bar(&G) is a call to
    // function Bar with pointer to G and
    // inferred template argument

    Foo(&Bar<G>); // don't work becuase the compiler could
    // not infer the template argument
    // type for overloadl function type from
    // overloaded function type

    Foo< Bar<G> >( &Bar<G> ); // doesn't work I suppose because
    // I should pass the template type as
    // pointer to Bar<G> but the compiler
    // complains as "invalid template
    // argument for FuncPtr; type
    // expected"

    typedef void (*GPtr) ();
    typedef void (*BarGPtr)(GPtr) ;
    Foo< BarGPtr >( &Bar<G> ); // doesn't work; compiler complains
    // that param 1 for Foo cannot be
    // converted from type void(T1) to
    // BarGPtr. I suppose T1 is a temporary
    // type?


    return 0;
    }

    Any ideas on this?

    thanks,

    -vijai.
     
    Vijai Kalyan, Nov 8, 2005
    #1
    1. Advertising

  2. Vijai Kalyan

    mlimber Guest

    Vijai Kalyan wrote:
    > I was decomposing a task into different policies. Essentially, there is
    > a general option obtained from a server and user options obtained from
    > configuration variables. The two options are complementary to one
    > another.
    >
    > So I proceeded to decompose the tasks that deal with user options into
    > two functions. Each of the functions do something and towards the end
    > they do supplementary tasks that depend on the server option.
    >
    > The whole things fits in a framework which notifies listeners before
    > and after the main tasks and so the notification scheme for the
    > listeners are separate policies as well.
    >
    > The whole thing is combinatorially complex and hence my decision to
    > split things up using templates as policies. Here is where I run into
    > my question: It is exemplified by the following simple code:
    >
    > #include <iostream>
    >
    > // main task function
    > template<class FuncPtr>
    > void Foo(FuncPtr ptr)
    > {
    > ptr();
    > }
    >
    > // want to pass an instance of this to Foo
    > template<class X>
    > void Bar(X x)
    > {
    > x();
    > }
    >
    > // want to pass pointer to this to Bar
    > void G()
    > {
    > std::wcout << "From G!" << std::endl;
    > }
    >
    > int main()
    > {
    > Foo(&Bar(&G)); // doesn't work, coz Bar(&G) is a call to
    > // function Bar with pointer to G and
    > // inferred template argument
    >
    > Foo(&Bar<G>); // don't work becuase the compiler could
    > // not infer the template argument
    > // type for overloadl function type from
    > // overloaded function type
    >
    > Foo< Bar<G> >( &Bar<G> ); // doesn't work I suppose because
    > // I should pass the template type as
    > // pointer to Bar<G> but the compiler
    > // complains as "invalid template
    > // argument for FuncPtr; type
    > // expected"
    >
    > typedef void (*GPtr) ();
    > typedef void (*BarGPtr)(GPtr) ;
    > Foo< BarGPtr >( &Bar<G> ); // doesn't work; compiler complains
    > // that param 1 for Foo cannot be
    > // converted from type void(T1) to
    > // BarGPtr. I suppose T1 is a temporary
    > // type?
    >
    >
    > return 0;
    > }
    >
    > Any ideas on this?
    >
    > thanks,
    >
    > -vijai.


    How about using a functor instead (only changed code is shown):

    template<class X>
    struct Bar
    {
    X x_;
    Bar( const X& x ) : x_( x ) {}
    void operator()()
    {
    x_();
    }
    };

    int main()
    {
    typedef void (*GPtr)();
    Foo( Bar<GPtr>(G) );
    return 0;
    }

    Cheers! --M
     
    mlimber, Nov 8, 2005
    #2
    1. Advertising

  3. Vijai Kalyan

    Vijai Kalyan Guest

    Yep, I thought about a functor, but I am not sure about using it purely
    from an aesthetic perspective. But thanks anyway. If nobody comes up
    with something then I will use a functor, but I still hope someone will
    know what the syntax is ... I googled it, but came up with pageful of
    results about function templates but not of pointers to them. It sure
    would be nice to know!

    regards,

    -vijai.
     
    Vijai Kalyan, Nov 8, 2005
    #3
  4. Vijai Kalyan

    John Carson Guest

    "Vijai Kalyan" <> wrote in message
    news:
    > I was decomposing a task into different policies. Essentially, there
    > is a general option obtained from a server and user options obtained
    > from configuration variables. The two options are complementary to one
    > another.
    >
    > So I proceeded to decompose the tasks that deal with user options into
    > two functions. Each of the functions do something and towards the end
    > they do supplementary tasks that depend on the server option.
    >
    > The whole things fits in a framework which notifies listeners before
    > and after the main tasks and so the notification scheme for the
    > listeners are separate policies as well.
    >
    > The whole thing is combinatorially complex and hence my decision to
    > split things up using templates as policies. Here is where I run into
    > my question: It is exemplified by the following simple code:
    >
    > #include <iostream>
    >
    > // main task function
    > template<class FuncPtr>
    > void Foo(FuncPtr ptr)
    > {
    > ptr();
    > }
    >
    > // want to pass an instance of this to Foo
    > template<class X>
    > void Bar(X x)
    > {
    > x();
    > }


    In that case, Foo is defined wrongly. If you pass an instance of Bar to Foo,
    then ptr will be &Bar<something> so

    ptr();

    translates into

    Bar<something>();

    which is wrong because Bar<something> has an X parameter. You must call:

    ptr(x);

    However, your Foo function isn't set up to be able to make sense of this x
    argument. Try this

    template<class BarFuncPtr, class GFunctPtr>
    void Foo(BarFuncPtr bptr, GFunctPtr gptr)
    {
    bptr(gptr);
    }

    // want to pass an instance of this to Foo
    template<class X>
    void Bar(X x)
    {
    x();
    }

    // want to pass pointer to this to Bar --- via Foo
    void G()
    {
    std::wcout << "From G!" << std::endl;
    }

    int main()
    {
    typedef void (*GPtr) ();
    Foo(&Bar<GPtr>, &G);
    return 0;
    }


    --
    John Carson
     
    John Carson, Nov 8, 2005
    #4
  5. Vijai Kalyan

    Vijai Kalyan Guest

    That's interesting. So, this means that Foo has to have knowledge of
    the type and structure of it's argument in order to understand that its
    second parameter has to be passed as an argument to its first
    parameter. But, that makes them strongly coupled right? What if I later
    wanted to use Foo with a function that was not a template?

    Anyway, this is interesting to know.

    Thanks for your help ... I finally went with Functors anyway!

    regards,

    -vijai.
     
    Vijai Kalyan, Nov 8, 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. nifsmith
    Replies:
    5
    Views:
    8,182
    Maitre Bart
    Sep 27, 2004
  2. Charles Sullivan

    Passing function pointer as argument to a function???

    Charles Sullivan, Sep 19, 2005, in forum: C Programming
    Replies:
    17
    Views:
    1,229
    Emmanuel Delahaye
    Sep 21, 2005
  3. nw
    Replies:
    0
    Views:
    314
  4. avasilev
    Replies:
    4
    Views:
    578
    avasilev
    Dec 22, 2011
  5. User1014
    Replies:
    1
    Views:
    186
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page